本文介绍了如何使用EF code首先Fluient API从主表中配置多个ForeignKey的referancing到同一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要设计使用桌面应用程序的现有数据库的Web应用程序。按照现有的数据库,我有以下类

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第一个默认框架FOREIGNKEY创建名称为CustomerCategory code_CategoryID,SupplierCategory code_CategoryID,国家code_Country codeID。我想用我的旧的数据库表例如相合这个名字FOREIGNKEY CustomerCategory codeID,SupplierCategory codeID,国家codeID。我怎么能使用EF code首先Fluient API做的。我尝试使用Fluient API映射做,但我得到了错误SupplierCategory code_Category codeID作为CustomerCategory code_CategoryID也被定位为同桌CompanyCategory code。此外,如果有使用数据注释然后也让我知道如何做到这一点任何可用的选项。

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.

推荐答案

您必须手动重新映射每个导航属性​​来定义它的关键。是这样的:

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; }
}

这篇关于如何使用EF code首先Fluient API从主表中配置多个ForeignKey的referancing到同一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-08 01:10