如何使用 EF Code First Fluient API 配置从主表引用同一个表的多个外键

发布于 2024-11-19 14:12:19 字数 1943 浏览 9 评论 0原文

我必须使用桌面应用程序的现有数据库设计一个网络应用程序。根据现有数据库,我有以下类

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

臻嫒无言 2024-11-26 14:12:20

您必须手动重新映射每个导航属性才能定义其键。类似于:

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

除非您在每个依赖实体中定义导航属性和外键属性,否则数据注释是不可能的,例如:

public class Company
{
    ...

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

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; }
}
浅忆 2024-11-26 14:12:20

在上下文类中,您需要重写 OnModelCreating 并映射键,它应该如下所示:

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

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"));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文