c# - Managing associations in Entity Framework Seed method -
i have class address
:
[table("address")] public class address { [key] [databasegeneratedattribute(databasegeneratedoption.identity)] public int addressid { get; set; } // ... // address belongs 1 residentialproperty public int residentialpropertyid { get; set; } [foreignkey("residentialpropertyid")] public virtual residentialproperty residentialproperty { get; set; } }
and class residentialproperty
:
[table("residentialproperty")] public class residentialproperty { [key] [databasegeneratedattribute(databasegeneratedoption.identity)] public int residentialpropertyid { get; set; } // ... }
one property can have 1 address there's 1:1 relationship between them. i'm trying add together multiple instances of each database using seed()
method , migrations. in seed()
method create instance of residentialproperty
this:
var residentialproperties = new list<residentialproperty> { // property 1 associated landlord 1 new residentialproperty { /* properties */ userprofile = landlords.single(u => u.userid == 11) }, // ... }
and instance of address
this:
var addresses = new list<address> { // address 1 associated property 1 new address { /* properties */ residentialproperty = residentialproperties.single(rp => rp.residentialpropertyid == 1) }, // ... }
when add together migration , update database neither of entities above added. in seed() method, have 4 other entities added successfully.
is there wrong way have association set up?
c# entity-framework ef-code-first ef-migrations seeding
No comments:
Post a Comment