ef code first - Two foreign keys to same primary table -


i have 2 classes: customer , association.

a customer can have association many customers. each association of defined type (family, friend, etc) i.e customer friend of customer b. customer related customer c. type of association defined enum associationtype.

in order create in ef i've defined following classes

public class customer {     public string firstname {get; set;}     public string lastname {get; set;}      public virtual icollection<association> associations { get; set; } }  public class association {     public int customerid { get; set; }     public virtual customer customer { get; set; }      public int associatedcustomerid { get; set; }     public virtual customer associatedcustomer { get; set; }         public associationtype associationtype { get; set; } } 

i've removed data annotations unable compile. error:

"model compatibility cannot checked because database not contain model metadata".

does have ideas?

it happens when error occurs during database creation. database schema created - except __migrationhistory table. when run application again ef wants check against __migrationhistory table if schema still up-to-date model , if table doesn't exist throws exception having.

to fix problem either delete database manually or set initializer dropcreatedatabasealways<mycontext> (with database.setinitializer(new dropcreatedatabasealways<mycontext>()) - once. after db created set original initializer.

btw: model have specify explicitly customer.associations related association.customer, either data annotations...

[inverseproperty("customer")] public virtual icollection<association> associations { get; set; } 

...or fluent api:

modelbuilder.entity<customer>()     .hasmany(c => c.associations)     .withrequired(a => a.customer)     .hasforeignkey(a => a.customerid); 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -