ef6 icon indicating copy to clipboard operation
ef6 copied to clipboard

Code First Missing foreign key in generated migration

Open hdesouky opened this issue 9 years ago • 1 comments

Their is a bug in Entity Framework 6 when generating migratiion code foreign key in certain heirarchy situatuin. I have the following heirarchy Base Child1 Child2 Child3 Child11 Child22

public class LinkTable
{
public int Id{get;set;}
public Child1 {get;set;}
public Child2{get;set;}
public Child3{get;set;}
}

All above heirarchy is created using TPC

The generated migration code will not generate foreign keys for Child1 or Child2, but will generate foreign key for Child3

Is this a bug or am i missing something

hdesouky avatar Dec 05 '16 17:12 hdesouky

This is by design. The reference to Child3 will always be to a row in the Child3 table - which can be represented with an FK constraint in a relational database. However, the relationship to Child2 could be pointing to a row in the Child2 or Child3 table - which can not be represented with an FK constraint. Same for Child1, it could be a row in Child1, Child2, or Child3 table.

EF still treats it as a relationship, it just can't create an FK constraint in the database to represent the relationship.

rowanmiller avatar Jan 20 '17 23:01 rowanmiller