c# - MVC5 Identity + multiple context in EntityFramework -
c# - MVC5 Identity + multiple context in EntityFramework -
i have mvc 5 application, uses default identity authentication. user profile part of our model, means there several class have foreign key userinfo. there 2 dbcontext, 1 model , userinfo.
public class applicationdbcontext : identitydbcontext<userinfo> { public applicationdbcontext() : base("profile") { } } public class userinfo : identityuser { public userinfo () { libraryitems = new list<libraryitem>(); if (usergroup == usergroupenum.analyst) { companies = new list<company>(); } datecreate = datetime.now; dateupdate = datetime.now; datedelete = datetime.now; } [displaycolumninindex] [displayname("is active ?")] public bool isactive { get; set; } [timestamp] public byte[] rowversion { get; set; } //[validatefile(errormessage = "please select png image smaller 1mb")] //[displayformat(applyformatineditmode = true, dataformatstring = "{0:mm/dd/yyyy}")] [editable(false, allowinitialvalue = true)] public datetime datecreate { get; set; } //[displayformat(applyformatineditmode = true, dataformatstring = "{0:mm/dd/yyyy}")] public datetime dateupdate { get; set; } //[displayformat(applyformatineditmode = true, dataformatstring = "{0:mm/dd/yyyy}")] public datetime datedelete { get; set; } [displaycolumninindex] public string name { get; set; } [displaycolumninindex] public string firstname { get; set; } [displaycolumninindex] public string pseudo { get; set; } [displaycolumninindex] public string phonenumber { get; set; } [displaycolumninindex] public string company { get; set; } [displayname("name_id")] [foreignkey("fullname")] public int? nameid { get; set; } [displaycolumninindex] public usergroupenum usergroup { get; set; } [displaycolumninindex] public userstatusenum userstatus { get; set; } public virtual name fullname { get; set; } } public class comment { // more properties [displayname("userinfoid")] [foreignkey("userinfo")] public virtual string userinfoid { get; set; } } public class { // more properties [displayname("userinfoid")] [foreignkey("userinfo")] public virtual string userinfoid { get; set; } }
my userinfo has foreign keys the other tables of sec dbontext.
what best way design our authentication system? , maintain relationship between 2 contexts.
thanks in advance.
the solution came utilize single context both asp.net identity info , business entities:
public class databasecontext : identitydbcontext<userinfo> { public virtual dbset<comment> comments { get; set; } // business entities public databasecontext() : base("name=databasecontext") { } }
notice databasecontext
inherits identitydbcontext<userinfo>
.
there trade-offs approach: example, info access layer should reference microsoft.aspnet.identity.core
, microsoft.aspnet.identity.entityframework
; however, having single database context in project makes things much easier if using dependency injection or entity framework migrations.
c# entity-framework asp.net-mvc-5 asp.net-identity
Comments
Post a Comment