Entity Framework multiple mapping to same table DB first

c# entity-framework-6

Popular Answer

For those two navigation properties, you must add the missing ForeignKey attributes to foreign keys:

[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, when using EF, avoid using generic names for navigation properties. In essence, one of the best things that EF offers you is the ability to say:

@myCustomer.Assigned.Name

etc. in the perspective, and names like Engineer and Engineer1 are completely ruining it.

Keep NOTE2:Nullable<int> the creation of code.int? is much easier to read.

Use the VS refactoring feature to rename properties.Engineer and Engineer1 compared to the ideal state (PrimaryEngineer and AssignedEningeer etc). Add after thatForeignKey characteristics of your model. That ought to be adequate. Any modifications you make in the future must be made in the code, not the database.

IF Making ensuring that all of your foreign keys have meaningful names is important since EF will utilize them to construct names when you are continually producing entities and context code from the database. i.e., it has no nameEngineer1 Unexpectedly) Rename the foreign keys to reflect their logical connections. The following foreign keys are most likely present in your database:

 FK_Customer_Engineer

 FK_Customer_Engineer1

You must give them a new name.

 FK_Customer_PrimaryEngineer

 FK_Customer_AssignedEngineer

You can have distinct column names and property names by doing something like this:

[Column("PrimaryEngId")]
[ForeignKey("Primary")]
public int? PrimaryID { get; set; }
2
3/14/2016 1:01:36 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow