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:

public int? PrimaryEngId { get; set; }

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:


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:



You must give them a new name.



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

public int? PrimaryID { get; set; }
3/14/2016 1:01:36 PM

Related Questions


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