.net - Define a foreign key on a CLR type or not define -
what best practice in entity framework:
1.define foreign key on clr type
public class book { public int authorid{get;set;} public author author{get;set;} }
2.not define foreign key(only define navigation property)
public class book { public author author{get;set;} }
my personal preference use define fk , set key value (authorid
) when defining relationship.
there's 2 great article here , here julie lerman explain of benefits of taking approach.
here's summary:
save grief , use foreign key
rather setting navigation property , having worry state of [related entity], set [id] property, because have access value in [related entity] instance. find myself suggesting developers. on twitter, see question: “why ef inserting data exists?” , guess correctly in reply: “any chance u r setting navigation property on new entity instead of fk? j”
Comments
Post a Comment