How to configure multiple Foreignkey referancing to same table from main table using EF Code First Fluient API

怎甘沉沦 提交于 2019-12-06 09:56:28

问题


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.


回答1:


You must manually remap each navigation property to define its key. Something like:

modelBuilder.Entity<Company>()
            .HasRequired(c => c.CountryCode)
            .WithMany()
            .HasForeignKey("CountryCodeID");

modelBuilder.Entity<Company>()
            .HasMany(c => c.CustomerCategoryCode)
            .WithOptional()
            .HasForeignKey("CustomerCategoryCodeID")
            .WillCascadeOnDelete(false);

modelBuilder.Entity<Company>()
            .HasMany(c => c.SupplierCategoryCode)
            .WithOptional()
            .HasForeignKey("SupplierCategoryCodeID")
            .WillCascadeOnDelete(false); 

It is not possible with data annotations unless you define navigation property and foreign key property in each dependent entity like:

public class Company
{
    ...

    [ForeignKey("CountryCode")]
    public virtual int CountryCodeID { get; set; }
    public CountryCode CountryCode { get; set; }
}



回答2:


Inside your context class, you will need to override OnModelCreating and map the keys, it should look something like this:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
             modelBuilder.Entity<Company>()
                .HasRequired(c => c.CustomerCategoryCode)
                .WithMany() 
                .Map(mc => mc.MapKey("CustomerCategoryCodeID"));
}


来源:https://stackoverflow.com/questions/6660067/how-to-configure-multiple-foreignkey-referancing-to-same-table-from-main-table-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!