c# - ApplicationUser is not part of the model for the current context -
i have asp mvc project got colleague , had 2 contexts. 1 template needed login , other 1 selfmade. want combine 2 error time:
applicationuser not part of model current context
the model database first. made sql tables first made ado.net entity data model gives connectionstring:
<add name="dairycowbarnentities" connectionstring="metadata=res://*/models.dairycowbarnmodel.csdl|res://*/models.dairycowbarnmodel.ssdl|res://*/models.dairycowbarnmodel.msl;provider=system.data.sqlclient;provider connection string="data source=server;initial catalog=dairycowbarn;integrated security=true;multipleactiveresultsets=true;app=entityframework"" providername="system.data.entityclient" />
in database required asp.net tables added , in data model. added asp.net tables via migration.
this dbcontext class:
public partial class dairycowbarnentities : identitydbcontext<applicationuser> { public dairycowbarnentities() : base("name=dairycowbarnentities") { } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { throw new unintentionalcodefirstexception(); } public static dairycowbarnentities create() { return new dairycowbarnentities(); } public virtual dbset<c__migrationhistory> c__migrationhistory { get; set; } public virtual dbset<c__refactorlog> c__refactorlog { get; set; } public virtual dbset<aspnetrole> aspnetroles { get; set; } public virtual dbset<aspnetuserclaim> aspnetuserclaims { get; set; } public virtual dbset<aspnetuserlogin> aspnetuserlogins { get; set; } public virtual dbset<aspnetuser> aspnetusers { get; set; } public virtual dbset<jntconcentratemixture> jntconcentratemixtures { get; set; } public virtual dbset<jntconsistingparcel> jntconsistingparcels { get; set; } }
i have read applicationuser needs normal connectionstring this:
<add name="dairycowbarnentities" connectionstring="data source=server;initial catalog=dairycowbarn;integrated security=true" providername="system.data.sqlclient" />
but i'm getting error:
the context being used in code first mode code generated edmx file either database first or model first development. not work correctly. fix problem not remove line of code throws exception. if wish use database first or model first, make sure entity framework connection string included in app.config or web.config of start-up project. if creating own dbconnection, make sure entityconnection , not other type of dbconnection, , pass 1 of base dbcontext constructors take dbconnection.
i have no idea how can combine two. i'm out of ideas. appreciated , needed. explained clearly. otherwise ask if need more info.
when reverse engineering on database, pull in aspnet tables, not need. model need regarding authentication
public class applicationuser : identityuser { public datetimeoffset createdon { get; set; } public async task<claimsidentity> generateuseridentityasync(usermanager<applicationuser> manager) { // note authenticationtype must match 1 defined in cookieauthenticationoptions.authenticationtype var useridentity = await manager.createidentityasync(this, defaultauthenticationtypes.applicationcookie); // add custom user claims here return useridentity; } }
i've added createdon column aspnetusers table, why in above model. next step make sure dbcontext derives identitydbcontext<applicationuser>
. base class handle mapping aspnet tables. dbcontext should like:
public partial class dairycowbarnentities : identitydbcontext<applicationuser> { public dairycowbarnentities() : base("name=dairycowbarnentities") { } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { throw new unintentionalcodefirstexception(); } public static dairycowbarnentities create() { return new dairycowbarnentities(); } public virtual dbset<jntconcentratemixture> jntconcentratemixtures { get; set; } public virtual dbset<jntconsistingparcel> jntconsistingparcels { get; set; } }
so this, remove models regarding aspnet , references models in code. add applicationuser model don't create dbset , pass derived class identitydbcontext<applicationuser>
. identitydbcontext handle authentication , authorization can use usermanager , identity normal.
a note, createdon database generated default (sysdatetimeoffset()) never worked. had explicitly set createdon in code or populated database datetimeoffset.minvalue.
updated answer comment:
the identitydbcontext<applicationuser>
db context derives handles mapping you. provides layer of abstraction don't have worry it. if need query tables beyond usermanager you, ie; getting users email addresses not confirmed, can write own select statement , pass dbcontext dbcontext.database.sqlquery<t>(sqlquery, false);
t in case model create should have same property names , types column names querying. example below.
public users { public string username { get; set; } public bool emailconfirmed { get; set; } } var sqlquery = "select users.username, users.emailconfirmed aspnetusers users.emailconfirmed = {0}" var unconfirmedusers = dbcontext.database.sqlquery<users>(sqlquery, false).tolist();
using {0}
, passing in value argument automatically scrub value no sql injection scripts work.
Comments
Post a Comment