c# - Use Proper UniqueIdentifier in SQL with Entity Framework Identity -
i have gone through lot of stackoverflow posts , tons of blogs yet unable figure out missing here....
using:
ef 6.1.3
aspnet.identity.core 2.2.1
aspnet.identity.entityframework 2.2.1
applicationuser:
[table("users", schema = "user")] public class applicationuser : identityuser<guid,applicationuserlogin, applicationuserrole, applicationuserclaim> { }
applicationuserclaim
[table("userclaims", schema = "user")] public class applicationuserclaim: identityuserclaim<guid> { }
applicationuserlogin
[table("userlogins", schema = "user")] public class applicationuserlogin: identityuserlogin<guid> { }
applicationuserrole
[table("userroles", schema = "user")] public class applicationuserrole: identityuserrole<guid> { }
applicationrole
[table("roles", schema = "application")] public class applicationrole: identityrole<guid, applicationuserrole> { }
applicationdbcontext
public class applicationdbcontext : identitydbcontext<applicationuser,applicationrole, guid, applicationuserlogin, applicationuserrole, applicationuserclaim> . . protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.hasdefaultschema("application"); base.onmodelcreating(modelbuilder); setupapplicationtables(modelbuilder); configuration.proxycreationenabled = false; configuration.lazyloadingenabled = false; } . . private void setupapplicationtables(dbmodelbuilder modelbuilder) { // application.roles table //modelbuilder.entity<identityrole>().totable("roles", "application"); //// user.userlogins table modelbuilder.entity<applicationuserlogin>().haskey(ul => new {ul.loginprovider, ul.providerkey, ul.userid}); modelbuilder.entity<applicationuserlogin>().totable("userlogins", "user"); //// user.users table //modelbuilder.entity<identityuser>().totable("users", "user"); //// user.userroles table modelbuilder.entity<applicationuserrole>().haskey(ur => new { ur.roleid, ur.userid}); modelbuilder.entity<applicationuserrole>().totable("userroles", "user"); //// user.userclaims table //modelbuilder.entity<identityuserclaim>().totable("userclaims", "user"); }
when try add-migration, following:
peoplesparty.dataaccesslayer.dbcontext.identityuserrole: : entitytype 'identityuserrole' has no key defined. define key entitytype. peoplesparty.dataaccesslayer.dbcontext.identityuserlogin: : entitytype 'identityuserlogin' has no key defined. define key entitytype. identityuserroles: entitytype: entityset 'identityuserroles' based on type 'identityuserrole' has no keys defined. identityuserlogins: entitytype: entityset 'identityuserlogins' based on type 'identityuserlogin' has no keys defined.
i have made several changes in attempt make work, still no luck:
// models [table("users", schema = "user")] public class applicationuser : identityuser<guid,applicationuserlogin, applicationuserrole, applicationuserclaim> { public applicationuser() : base() { } } [table("roles", schema = "application")] public class applicationrole : identityrole<guid, applicationuserrole> { public applicationrole() { id = guid.newguid(); } public applicationrole(string name) { name = name; id = guid.newguid(); } } [table("userclaims", schema = "user")] public class applicationuserclaim: identityuserclaim<guid> { public applicationuserclaim() : base() { } } [table("userlogins", schema = "user")] public class applicationuserlogin: identityuserlogin<guid> { public applicationuserlogin() : base() { } } [table("userroles", schema = "user")] public class applicationuserrole : identityuserrole<guid> { public applicationuserrole() : base() { } } // db context public class applicationdbcontext : identitydbcontext<applicationuser, applicationrole, guid, applicationuserlogin, applicationuserrole, applicationuserclaim> { static applicationdbcontext() { database.setinitializer<applicationdbcontext>(null); } public applicationdbcontext() : this("peoplespartydb2") { } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { configuration.proxycreationenabled = false; configuration.lazyloadingenabled = false; modelbuilder.hasdefaultschema("application"); base.onmodelcreating(modelbuilder); setupcustomidentitytables(modelbuilder); } private void setupcustomidentitytables(dbmodelbuilder modelbuilder) { if (modelbuilder == null) { throw new argumentnullexception(nameof(modelbuilder)); } // needed ensure subclasses share same table var user = modelbuilder.entity<applicationuser>().haskey(au => au.id).totable("users", "user"); user.hasmany(u => u.roles).withrequired().hasforeignkey(ur => ur.userid); user.hasmany(u => u.claims).withrequired().hasforeignkey(uc => uc.userid); user.hasmany(u => u.logins).withrequired().hasforeignkey(ul => ul.userid); user.property(u => u.username) .isrequired() .hasmaxlength(256) .hascolumnannotation("index", new indexannotation(new indexattribute("usernameindex") {isunique = true})); // consider: u.email required if set on options? user.property(u => u.email).hasmaxlength(256); modelbuilder.entity<applicationuserrole>().haskey(ur => new {ur.userid, ur.roleid}).totable("userroles", "user"); modelbuilder.entity<applicationuserlogin>() .haskey(ul => new {ul.loginprovider, ul.providerkey, ul.userid}) .totable("userlogins", "user"); modelbuilder.entity<applicationuserclaim>().haskey(uc => uc.id).totable("userclaims", "user"); var role = modelbuilder.entity<applicationrole>().haskey(ar => ar.id).totable("roles", "application"); role.hasmany(r => r.users).withrequired().hasforeignkey(ur => ur.roleid); role.property(r => r.name) .isrequired() .hasmaxlength(256) .hascolumnannotation("index", new indexannotation(new indexattribute("rolenameindex") {isunique = true})); } }