c# - Navigation property without referential integrity constraint in Entity Framework 6 -


in ef 6 (code first), possible have navigation property set-up without enforcing referential integrity constraint?

for example:

public class person{      public ilist<pet> pets { get; set; }  }  public class pet{      public int ownerid { get; set; }     public person owner { get; set; }  } 

so in example above i'd able add pet ownerid, if owner not exist in owners table.

thanks

matt

you can define relationship using fluent api.

modelbuilder.entity<pet>     .hasoptional(p => p.owner)     .willcascadeondelete(false); 

this configure relational property optional, , ensure cascade delete not take effect. can create pet without owner, , deleting owner not delete associated pets.

however cannot assign pet.ownerid ownerid doesn't exist in owner table. if need have way of tracking invalid ownerid values, either need have separate property manually update arbitrary value, or need define these objects without using navigation property, , perform lookups manually.

it exceptional situation need supply arbitrary value ownerid doesn't match owner table; in 99% of cases, optional relationship accepts valid ownerid or null necessary.

the ownerid property isn't necessary on pet object, if present, should set int? nullable.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -