如何使用 EF Code First Fluient API 配置从主表引用同一个表的多个外键
我必须使用桌面应用程序的现有数据库设计一个网络应用程序。根据现有数据库,我有以下类
public class Company
{
#region Primitive Properties
public virtual int CompanyID { get; set; }
public virtual string CompanyName { get; set; }
public virtual bool IsCustomer { get; set; }
public virtual string CustomerCode { get; set; }
public virtual bool IsPotentialCustomer { get; set; }
public virtual bool IsSupplier { get; set; }
public virtual string SupplierCode { get; set; }
public virtual bool IsPotentialSupplier { get; set; }
public CompanyCategoryCodes CustomerCategoryCode { get; set; }
public CompanyCategoryCodes SupplierCategoryCode { get; set; }
public CountryCode CountryCode { get; set; }
}
public class CompanyCategoryCodes
{
public virtual int CategoryID { get; set; }
public virtual string CategoryCodes { get; set; }
public virtual bool PotentialCustomer { get; set; }
public virtual bool PotentialSupplier { get; set; }
public virtual System.DateTime LastModifiedDate { get; set; }
public virtual bool Manufacturer { get; set; }
}
public class CountryCode
{
public virtual int CountryCodeID { get; set; }
public virtual string Code { get; set; }
public virtual string Description { get; set; }
public virtual bool DefaultCode { get; set; }
public virtual bool EECVATApplies { get; set; }
public virtual System.DateTime LastModifiedDate { get; set; }
public virtual bool FixedAddressFormat { get; set; }
}
EF Code 第一个默认框架正在创建名称为“CustomerCategoryCode_CategoryID”、“SupplierCategoryCode_CategoryID”、“CountryCode_CountryCodeID”的外键。我希望这个外键名称与我的旧数据库表一致,例如“CustomerCategoryCodeID”、“SupplierCategoryCodeID”、“CountryCodeID”。我如何使用 EF Code First Fluient API 来做到这一点。我尝试使用 Fluient API 映射来执行此操作,但出现“SupplierCategoryCode_CategoryCodeID”错误,因为“CustomerCategoryCode_CategoryID”也定位到同一个表“CompanyCategoryCode”。另外,如果有任何使用数据注释的选项可用,那么也请让我知道如何实现这一点。
I have to design a web application using existing database of desktop application. As per existing database i have below class
public class Company
{
#region Primitive Properties
public virtual int CompanyID { get; set; }
public virtual string CompanyName { get; set; }
public virtual bool IsCustomer { get; set; }
public virtual string CustomerCode { get; set; }
public virtual bool IsPotentialCustomer { get; set; }
public virtual bool IsSupplier { get; set; }
public virtual string SupplierCode { get; set; }
public virtual bool IsPotentialSupplier { get; set; }
public CompanyCategoryCodes CustomerCategoryCode { get; set; }
public CompanyCategoryCodes SupplierCategoryCode { get; set; }
public CountryCode CountryCode { get; set; }
}
public class CompanyCategoryCodes
{
public virtual int CategoryID { get; set; }
public virtual string CategoryCodes { get; set; }
public virtual bool PotentialCustomer { get; set; }
public virtual bool PotentialSupplier { get; set; }
public virtual System.DateTime LastModifiedDate { get; set; }
public virtual bool Manufacturer { get; set; }
}
public class CountryCode
{
public virtual int CountryCodeID { get; set; }
public virtual string Code { get; set; }
public virtual string Description { get; set; }
public virtual bool DefaultCode { get; set; }
public virtual bool EECVATApplies { get; set; }
public virtual System.DateTime LastModifiedDate { get; set; }
public virtual bool FixedAddressFormat { get; set; }
}
EF Code first default framework is creating Foreignkey with name "CustomerCategoryCode_CategoryID" , "SupplierCategoryCode_CategoryID", "CountryCode_CountryCodeID". I want this Foreignkey name to be consistance with my old database tables e.g. "CustomerCategoryCodeID", "SupplierCategoryCodeID", "CountryCodeID". How can i do it using EF Code First Fluient API. I try to do it using Fluient API Mapping but i got error for "SupplierCategoryCode_CategoryCodeID" as "CustomerCategoryCode_CategoryID" is also locating to same table "CompanyCategoryCode". Also if is there any option available using Data Annotation then also let me know how to achieve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须手动重新映射每个导航属性才能定义其键。类似于:
除非您在每个依赖实体中定义导航属性和外键属性,否则数据注释是不可能的,例如:
You must manually remap each navigation property to define its key. Something like:
It is not possible with data annotations unless you define navigation property and foreign key property in each dependent entity like:
在上下文类中,您需要重写 OnModelCreating 并映射键,它应该如下所示:
Inside your context class, you will need to override OnModelCreating and map the keys, it should look something like this: