简体   繁体   English

实体框架首先多个映射到同一个表DB

[英]Entity Framework multiple mapping to same table DB first

I have an issue with an Entity Framework from DB model. 我有一个来自DB模型的实体框架的问题。

My issue is down to the fact that one of my models has a multiple references to one table. 我的问题是由于我的一个模型对一个表有多个引用。

public partial class Customer
{
    public int Id { get; set; }

    public Nullable<int> PrimaryEngId { get; set; }
    public Nullable<int> AssignedDevloperId { get; set; }

    public virtual Engineer Engineer { get; set; }
    public virtual Engineer Engineer1 { get; set; }
}

In my model the columns are mapped respectively, however when a colleague builds the model from the same database the two are reversed. 在我的模型中,列分别被映射,但是当同事从同一数据库构建模型时,两者是相反的。

I believe the issue is that the first mapping to in was the primaryEngId and the Db constraint is called FK_Customer_Engineer . 我认为问题是第一个映射到的是primaryEngId ,Db约束叫做FK_Customer_Engineer

And the assigned developer id was added subsequently and the DB constraint is called FK_Customer_Devloper 随后添加了分配的开发人员ID,DB约束称为FK_Customer_Devloper

So alphabetically Developer come before Engineer and Entity Framework now maps them the other way round. 因此按字母顺序Developer EngineerEngineer和实体框架之前就已经反过来了。

My code references the Engineer in quite a lot of places which now won't work 我的代码在相当多的地方引用了Engineer ,现在它们无法工作

Is there any way out of this? 这有什么办法吗?

Many thanks 非常感谢

Ian 伊恩

You have to add missing ForeignKey attributes on foreign keys for those two navigation properties: 您必须在外键上为这两个导航属性添加缺少的ForeignKey属性:

[ForeignKey("Primary")]
public int? PrimaryEngId { get; set; }

[ForeignKey("Assigned")]    
public int? AssignedDevloperId { get; set; }

public virtual Engineer Primary { get; set; }

public virtual Engineer Assigned { get; set; }

NOTE : Also don't use generic names for navigation properties with EF. 注意 :也不要使用EF的导航属性的通用名称。 In the nutshell one of the best things EF gives you is that you can say: 简而言之,EF给你的最好的东西之一就是你可以说:

@myCustomer.Assigned.Name

etc in the view, and you are totally screwing it up with names like Engineer and Engineer1. 在视图中等等,你完全搞砸了像Engineer和Engineer1这样的名字。

NOTE2 : Keep Nullable<int> to code generation. 注意2 :保持Nullable<int>代码生成。 int? is a lot more readable. 更具可读性。

NOTE3 : Use VS refactoring to rename properties Engineer and Engineer1 to what they should be ( PrimaryEngineer and AssignedEningeer etc). 注3 :使用VS重构将属性EngineerEngineer1重命名为它们应该是什么( PrimaryEngineerAssignedEningeer等)。 After that add ForeignKey attributes to your model. 之后,将ForeignKey属性添加到模型中。 That should be enough. 那应该够了。 However, any future changes that you are doing has to be done in the Code and not in db. 但是,您正在进行的任何未来更改都必须在代码中完成,而不是在db中完成。

IF on the other hand you are constantly regenerating entities and context code from database, make sure that all your foreign keys has meaningful names, as EF will use them to generate name.(ie it is not named Engineer1 out of blue) Rename those foreign keys to reflect what logical relationship is. 如果在另一方面,你不断地新生从数据库实体和上下文代码,请确保您的所有外键具有有意义的名称,如EF将使用它们生成的名字。(即不命名Engineer1出蓝色)重新命名的外国用于反映逻辑关系的键。 Ie you most likely have the following foreign keys in db: 也就是说,你很可能在db中有以下外键:

 FK_Customer_Engineer

 FK_Customer_Engineer1

You need to rename them to 您需要将它们重命名为

 FK_Customer_PrimaryEngineer

 FK_Customer_AssignedEngineer

Update : you can have different column name and property name like so: 更新 :您可以使用不同的列名和属性名称,如下所示:

[Column("PrimaryEngId")]
[ForeignKey("Primary")]
public int? PrimaryID { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM