entity framework - How to add a foreign key reference to ASP.Net MVC 5 Identity? -
i have new asp.net mvc 5.1 project using asp.net identity. seems reliable , promise, today spend 9 hours simple things if using sql.
my problem is, cannot create table via codefirst foreign key reference default aspnetusers table.
for example: have create table named - address
public class address { [key] public string userid { get; set; } public string myaddress { get; set; } }
but how can create foreign key reference aspnetusers?
i try replace property above by
public identityuser myaddress { get; set; } // or public virtual identityuser myaddress { get; set; } // or public virtual applicationuser myaddress { get; set; }
but of them still show error:
one or more validation errors detected during model generation: miapos.models.identityuserlogin: : entitytype 'identityuserlogin' has no key defined. define key entitytype. miapos.models.identityuserrole: : entitytype 'identityuserrole' has no key defined. define key entitytype. identityuserlogins: entitytype: entityset 'identityuserlogins' based on type 'identityuserlogin' has no keys defined. identityuserroles: entitytype: entityset 'identityuserroles' based on type 'identityuserrole' has no keys defined.
i try override onmodelcreating post
protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<identityuserlogin>().haskey<string>(l => l.userid); modelbuilder.entity<identityrole>().haskey<string>(r => r.id); modelbuilder.entity<identityuserrole>().haskey(r => new { r.roleid, r.userid }); }
but when run add-migration, create identityuserlogin, identityrole, identityuserrole tables , duplicate, prefix different. (aspnet<->identity)
finally sql in 10 seconds, want know why cannot in codefirst? , why hard such things microsoft 2014 new technology.
you can this. because sometime may need 1-1 connection
public class anothertable { [key] public string userid { get; set; } [foreignkey("userid")] public virtual applicationuser user { get; set; } }
Comments
Post a Comment