asp.net mvc - One to many with applicationuser -
if dupp, means direct me answer.
asp.net, mvc5, ef6
each application user can have many tfsaccountcredentials
identitymodel.cs has following:
public class applicationuser : identityuser { public virtual icollection<tfsaccountcredentials> tfsaccountcredentials { get; set; } }
tfsaccountcredentials.cs has following:
public class tfsaccountcredentials { public int id { get; set; } //other properties [foreignkey("applicationuserid")] public virtual applicationuser applicationuser { get; set; } public string applicationuserid { get; set; } }
tfsaccountcredentialsdb has following:
public class tfsaccountcredentialsdb : dbcontext { public tfsaccountcredentialsdb() : base("defaultconnection") { } public dbset<tfsaccountcredentials> tfsaccountcredentials { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { base.onmodelcreating(modelbuilder); modelbuilder.entity<tfsaccountcredentials>() .hasrequired(ac => ac.applicationuser) .withmany(ac => ac.tfsaccountcredentials) .hasforeignkey(ac => ac.applicationuserid); } }
identitydb.cs has following:
public class identitydb : identitydbcontext<applicationuser> { public identitydb() : base("defaultconnection") { } }
two things. first, when add migration using identity context, part of script generates adds tfsaccountcredentials table, exists due earlier migration tfsaccountcredentials context.
second, when add migration using tfsaccountcredentials context, following error:
tfsteamstatus.web.datacontexts.identityuserlogin: : entitytype 'identityuserlogin' has no key defined. define key entitytype. tfsteamstatus.web.datacontexts.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.
edit final solution:
identitymodel.cs contains following:
public class applicationuser : identityuser { public virtual icollection<tfsaccountcredentials> tfsaccountcredentials { get; set; } }
tfsaccountcredentials.cs contains following:
public class tfsaccountcredentials { public int id { get; set; } //other properties [foreignkey("applicationuserid")] public virtual applicationuser applicationuser { get; set; } public string applicationuserid { get; set; } }
applicationdb.cs contains following:
public class applicationdb : identitydbcontext<applicationuser> { public applicationdb() : base("defaultconnection") { } public dbset<tfsaccountcredentials> tfsaccountcredentials { get; set; } }
so tfsaccountcredentialsdb.cs , identitydb.cs got combined.
why making separate db? think want make 2 table in same db not different db remove tfsaccountcredentialsdb
, add tfsaccountcredentials
identitydb
:
public class identitydb : identitydbcontext<applicationuser> { public identitydb() : base("defaultconnection") { } public dbset<tfsaccountcredentials> tfsaccountcredentials { get; set; } }
Comments
Post a Comment