c# - Integrating ASP.NET Identity to existing database -
i'm new entity framework stuff, excuse me in advance.
i'm trying integrate asp.net identity project. i've got existing database, mapped , ready, , i'm using code first automatic migrations. i'm trying have identity store username, password , other fields , rest of information needs stored in table, called contact, need 1:1 connection identity table.
what i've tried far merging in 1 context, i've got is:
public partial class pmdb02context : identitydbcontext { static pmdb02context() { database.setinitializer<pmdb02context>(null); } public pmdb02context() : base("pmdb02context") { } --- lots of dbsets --- public dbset<applicationuser> applicationusers { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { base.onmodelcreating(modelbuilder); --- lots of mappings --- 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 }); modelbuilder.entity<applicationuser>().totable("identityusers", "dbo"); } }
when to:
var result = await usermanager.createasync(user, model.contactdata.password);
i receive this:
identityuser_logins_target: : multiplicity not valid in role 'identityuser_logins_target' in relationship 'identityuser_logins'. because dependent role refers key properties, upper bound of multiplicity of dependent role must '1'. description: unhandled exception occurred during execution of current web request. please review stack trace more information error , originated in code. exception details: system.data.entity.modelconfiguration.modelvalidationexception: 1 or more validation errors detected during model generation:
my applicationuser class looks this:
public class applicationuser : identityuser { public int contactid { get; set; } public bool? iscurrent { get; set; } public virtual contact contact { get; set; } }
so, need relation contact. problem, guess, in way i'm trying these things work @ all, i've been trying day yesterday , today well.
i'd appreciate help. thanks!
finally fixed issue yesterday. turns out main source of problem in line:
database.setinitializer<pmdb02context>(null);
when changed to:
database.setinitializer<pmdb02context>(new migratedatabasetolatestversion<pmdb02context, configuration>());
identity worked supposed to, added relationship contact usual.
Comments
Post a Comment