Foreign Keys referencing same table exception when upgrading from EF Core RC2 to V1 -
hopefully simple 1 have upgraded release version of ef core , can no longer run code against db.
i have 2 tables, client table , language table. client has 2 references language table, 1 language , other language @ home. language has
public icollection<client> clients { get; set; }
and client has
public language language { get; set; } private int? _languageid; public int? languageid { { if (_languageid != 0) return _languageid; if (language != null) return language.languageid; return null; } set { _languageid = value; } } public language languageathome { get; set; } private int? _languageathomeid; public int? languageathomeid { { if (_languageathomeid != 0) return _languageathomeid; if (languageathome != null) return languageathome.languageid; return null; } set { _languageathomeid = value; } }
in onmodelcreating have following 2 lines
modelbuilder.entity<client>().hasone(m => m.languageathome).withmany(m => m.clients).hasforeignkey(p => p.languageathomeid).hasconstraintname("foreignkey_client_languageathome"); modelbuilder.entity<client>().hasone(m => m.language).withmany(m => m.clients).hasforeignkey(p => p.languageid).hasconstraintname("foreignkey_client_language");
i have looked @ upgrade documentation rc2 v1 , doesn't changes this. https://docs.efproject.net/en/latest/miscellaneous/rc2-rtm-upgrade.html
the exception is:
system.invalidoperationexception: cannot create relationship between 'language.clients' , 'client.language', because there relationship between 'language.clients' , 'client.languageathome'. navigation properties can participate in single relationship.
i tried reversing
modelbuilder.entity<language>().hasmany(l => l.clients).withone(c => c.languageathome).hasforeignkey(k => k.languageathomeid).hasconstraintname("foreignkey_client_languageathome"); modelbuilder.entity<language>().hasmany(l => l.clients).withone(c => c.language).hasforeignkey(k => k.languageid).hasconstraintname("foreignkey_client_language");
but exact same error.
looking @ ef source on git hub traced error internal relationship builder line 2145 in changeset https://github.com/aspnet/entityframework/commit/5765564bc4dc55f9acb1716a1f5b40a8f8b0b399.
my hunch change has occurred between rc2 , v1.
my question is doing wrong or bug has been introduced?
update -- solved andriy
i changed language class have 2 collections of client so
public icollection<client> languageathomeclients { get; set; } public icollection<client> languageclients { get; set; }
and modified relationship in onmodelcreating to
modelbuilder.entity<client>().hasone(m => m.languageathome).withmany(m => m.languageathomeclients).hasforeignkey(p => p.languageathomeid).hasconstraintname("foreignkey_client_languageathome"); modelbuilder.entity<client>().hasone(m => m.language).withmany(m => m.languageclients).hasforeignkey(p => p.languageid).hasconstraintname("foreignkey_client_language");
i upgraded ef core v1 , works treat!
as exception states navigation property can part of 1 relationship. in rc2 ef silently replace first 1 second one, in rtm throws alert fact. should create property, clientsathome other relationship.
Comments
Post a Comment