c# - ApplicationUser DbContext Merge with My DBcontext -


i have application that, have realised, stupidly has 2 separate dbcontexts , need merged in order allow relationships between application entities , applicationuser entity.

along development of application believe reference applicationuser within other classes has created tables applicationuser entity on dbcontext, whilst on different connection string , context exists tables aspuseridentity ect.

i have tried remove original context , merge 2 contexts when trying create migration wants rename existing applicationuser tables in current context aspuseridentity ect (see below)

how can merge contexts or there solution?

i'm not sure post try post feel relevant , if needs further information feel free request it.

this code after attempted merge. (using guide in 1 of answer question : merge mydbcontext asp.net identitydbcontext)

my context has following tables: applicationusers identityroles identityuserclaims identityuserlogins identityuserroles

the original context has following tables , migration trying rename above tables match these: aspnetroles aspnetuserclaims aspnetuserlogins aspnetuserroles aspnetusers

my context: (the commented modelbuilder statements causing multiplicity errors )

using system; using system.collections.generic; using system.linq; using system.web;  using system.data.entity; using system.componentmodel.dataannotations; using system.componentmodel.dataannotations.schema; using system.configuration; using system.web.mvc; using microsoft.aspnet.identity.entityframework; using system.data.entity.modelconfiguration.conventions; using ecommsite.models; using system.data.entity.infrastructure;  namespace ecommsite.areas.admin.models {     public class thedbcontext : identitydbcontext      {          public thedbcontext()                 : base(configurationmanager.connectionstrings["datadbstring"].connectionstring)             {                 var objectcontext = (this iobjectcontextadapter).objectcontext;                  // sets command timeout commands                 objectcontext.commandtimeout = 360;             }         public dbset<productbase> productbases { get; set; }         public dbset<address> addresses { get; set; }         public dbset<promotionimage> promotionimages { get; set; }         public dbset<image> images { get; set; }         public dbset<imagecollection> imagecollections { get; set; }         public dbset<order> orders { get; set; }         public dbset<orderline> orderlines { get; set; }         public dbset<productoption> productoptions { get; set; }         public dbset<productoptiontype> productoptiontypes { get; set; }         public dbset<producttype> producttypes { get; set; }         public dbset<filtervalueentry> filtervalueentries { get; set; }         public dbset<filtertype> filtertypesdb { get; set; }         public dbset<cardbasesummary> cardbasesummarys { get; set; }         public dbset<productbasesummary> productbasesummaries { get; set; }         public dbset<category> categories { get; set; }         public dbset<brand> brands { get; set; }         public dbset<mmhpriceentry> mmhpriceentries { get; set; }         public dbset<legality> legalities { get; set; }         public dbset<foreignname> foreignnames { get; set; }         public dbset<skustockquantity> skustockquantities { get; set; }         public dbset<location> locations { get; set; }         public dbset<cart> carts { get; set; }         public dbset<stockcollection> stockcollections { get; set; }          public static thedbcontext create()         {             return new thedbcontext();         }          protected override void onmodelcreating(dbmodelbuilder modelbuilder)         {             base.onmodelcreating(modelbuilder);              modelbuilder.conventions.remove<manytomanycascadedeleteconvention>();             modelbuilder.conventions.remove<onetomanycascadedeleteconvention>();             //  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 });          }          public system.data.entity.dbset<ecommsite.areas.admin.models.applicationuser> applicationusers { get; set; }          public system.data.entity.dbset<ecommsite.areas.admin.models.productrelation> productrelations { get; set; }          public system.data.entity.dbset<ecommsite.areas.admin.models.mtg_cards> mtg_cards { get; set; }          public system.data.entity.dbset<ecommsite.areas.admin.models.mtg_set> mtg_set { get; set; }          public system.data.entity.dbset<ecommsite.areas.admin.models.productoptionvalue> productoptionvalues { get; set; }          public system.data.entity.dbset<ecommsite.areas.admin.models.sku> skus { get; set; }          public system.data.entity.dbset<ecommsite.areas.admin.models.mmhsettojsonsetreference> mmhsettojsonsetreferences { get; set; }      } } 

the migration:

namespace ecommsite.migrations {     using system;     using system.data.entity.migrations;      public partial class mergemigration : dbmigration     {         public override void up()         {             renametable(name: "dbo.applicationusers", newname: "aspnetusers");             renametable(name: "dbo.identityuserclaims", newname: "aspnetuserclaims");             renametable(name: "dbo.identityuserlogins", newname: "aspnetuserlogins");             renametable(name: "dbo.identityuserroles", newname: "aspnetuserroles");             renametable(name: "dbo.identityroles", newname: "aspnetroles");             dropindex("dbo.aspnetuserclaims", new[] { "applicationuser_id" });             dropindex("dbo.aspnetuserlogins", new[] { "applicationuser_id" });             dropindex("dbo.aspnetuserroles", new[] { "applicationuser_id" });             dropindex("dbo.aspnetuserroles", new[] { "identityrole_id" });             dropcolumn("dbo.aspnetuserclaims", "userid");             dropcolumn("dbo.aspnetuserlogins", "userid");             dropcolumn("dbo.aspnetuserroles", "userid");             dropcolumn("dbo.aspnetuserroles", "roleid");             renamecolumn(table: "dbo.aspnetuserclaims", name: "applicationuser_id", newname: "userid");             renamecolumn(table: "dbo.aspnetuserlogins", name: "applicationuser_id", newname: "userid");             renamecolumn(table: "dbo.aspnetuserroles", name: "applicationuser_id", newname: "userid");             renamecolumn(table: "dbo.aspnetuserroles", name: "identityrole_id", newname: "roleid");             dropprimarykey("dbo.aspnetuserlogins");             dropprimarykey("dbo.aspnetuserroles");             addcolumn("dbo.aspnetusers", "discriminator", c => c.string(nullable: false, maxlength: 128));             altercolumn("dbo.aspnetusers", "newsletter", c => c.boolean());             altercolumn("dbo.aspnetusers", "rewardpoints", c => c.int());             altercolumn("dbo.aspnetusers", "basketid", c => c.int());             altercolumn("dbo.aspnetusers", "buylistbasketid", c => c.int());             altercolumn("dbo.aspnetusers", "storecredit", c => c.double());             altercolumn("dbo.aspnetusers", "email", c => c.string(maxlength: 256));             altercolumn("dbo.aspnetusers", "username", c => c.string(nullable: false, maxlength: 256));             altercolumn("dbo.aspnetuserclaims", "userid", c => c.string(nullable: false, maxlength: 128));             altercolumn("dbo.aspnetuserclaims", "userid", c => c.string(nullable: false, maxlength: 128));             altercolumn("dbo.aspnetuserlogins", "loginprovider", c => c.string(nullable: false, maxlength: 128));             altercolumn("dbo.aspnetuserlogins", "providerkey", c => c.string(nullable: false, maxlength: 128));             altercolumn("dbo.aspnetuserlogins", "userid", c => c.string(nullable: false, maxlength: 128));             altercolumn("dbo.aspnetuserroles", "userid", c => c.string(nullable: false, maxlength: 128));             altercolumn("dbo.aspnetuserroles", "roleid", c => c.string(nullable: false, maxlength: 128));             altercolumn("dbo.aspnetroles", "name", c => c.string(nullable: false, maxlength: 256));             addprimarykey("dbo.aspnetuserlogins", new[] { "loginprovider", "providerkey", "userid" });             addprimarykey("dbo.aspnetuserroles", new[] { "userid", "roleid" });             createindex("dbo.aspnetusers", "username", unique: true, name: "usernameindex");             createindex("dbo.aspnetuserclaims", "userid");             createindex("dbo.aspnetuserlogins", "userid");             createindex("dbo.aspnetuserroles", "userid");             createindex("dbo.aspnetuserroles", "roleid");             createindex("dbo.aspnetroles", "name", unique: true, name: "rolenameindex");         }          public override void down()         {             dropindex("dbo.aspnetroles", "rolenameindex");             dropindex("dbo.aspnetuserroles", new[] { "roleid" });             dropindex("dbo.aspnetuserroles", new[] { "userid" });             dropindex("dbo.aspnetuserlogins", new[] { "userid" });             dropindex("dbo.aspnetuserclaims", new[] { "userid" });             dropindex("dbo.aspnetusers", "usernameindex");             dropprimarykey("dbo.aspnetuserroles");             dropprimarykey("dbo.aspnetuserlogins");             altercolumn("dbo.aspnetroles", "name", c => c.string());             altercolumn("dbo.aspnetuserroles", "roleid", c => c.string(maxlength: 128));             altercolumn("dbo.aspnetuserroles", "userid", c => c.string(maxlength: 128));             altercolumn("dbo.aspnetuserlogins", "userid", c => c.string(maxlength: 128));             altercolumn("dbo.aspnetuserlogins", "providerkey", c => c.string());             altercolumn("dbo.aspnetuserlogins", "loginprovider", c => c.string());             altercolumn("dbo.aspnetuserclaims", "userid", c => c.string(maxlength: 128));             altercolumn("dbo.aspnetuserclaims", "userid", c => c.string());             altercolumn("dbo.aspnetusers", "username", c => c.string());             altercolumn("dbo.aspnetusers", "email", c => c.string());             altercolumn("dbo.aspnetusers", "storecredit", c => c.double(nullable: false));             altercolumn("dbo.aspnetusers", "buylistbasketid", c => c.int(nullable: false));             altercolumn("dbo.aspnetusers", "basketid", c => c.int(nullable: false));             altercolumn("dbo.aspnetusers", "rewardpoints", c => c.int(nullable: false));             altercolumn("dbo.aspnetusers", "newsletter", c => c.boolean(nullable: false));             dropcolumn("dbo.aspnetusers", "discriminator");             addprimarykey("dbo.aspnetuserroles", new[] { "roleid", "userid" });             addprimarykey("dbo.aspnetuserlogins", "userid");             renamecolumn(table: "dbo.aspnetuserroles", name: "roleid", newname: "identityrole_id");             renamecolumn(table: "dbo.aspnetuserroles", name: "userid", newname: "applicationuser_id");             renamecolumn(table: "dbo.aspnetuserlogins", name: "userid", newname: "applicationuser_id");             renamecolumn(table: "dbo.aspnetuserclaims", name: "userid", newname: "applicationuser_id");             addcolumn("dbo.aspnetuserroles", "roleid", c => c.string(nullable: false, maxlength: 128));             addcolumn("dbo.aspnetuserroles", "userid", c => c.string(nullable: false, maxlength: 128));             addcolumn("dbo.aspnetuserlogins", "userid", c => c.string(nullable: false, maxlength: 128));             addcolumn("dbo.aspnetuserclaims", "userid", c => c.string());             createindex("dbo.aspnetuserroles", "identityrole_id");             createindex("dbo.aspnetuserroles", "applicationuser_id");             createindex("dbo.aspnetuserlogins", "applicationuser_id");             createindex("dbo.aspnetuserclaims", "applicationuser_id");             renametable(name: "dbo.aspnetroles", newname: "identityroles");             renametable(name: "dbo.aspnetuserroles", newname: "identityuserroles");             renametable(name: "dbo.aspnetuserlogins", newname: "identityuserlogins");             renametable(name: "dbo.aspnetuserclaims", newname: "identityuserclaims");             renametable(name: "dbo.aspnetusers", newname: "applicationusers");         }     } } 

the error when running update-database:

the object 'pk_dbo.aspnetuserlogins' dependent on column 'userid'. alter table drop column userid failed because 1 or more objects access column. 

i know not expecting such answer, strong recommendation not merge. let identitydbcontext live live, , let live too. (only) con. need 2 separate db cost in azure know, still...

  1. if try live in merged context identitydbcontext cost many hours discover behaviour, , ef usage. spare time yourself.
  2. more: api identities (users, roles, login, lockout, etc) explicit classes , methods, not ef. strictly speaking should not know persistence (ef + rdbms) of identity subsystem. (consider black box)
  3. you right relationships, again: solution if have black box identity subsystem, provide services (including user/pwd/profile persistance) , build relational db business case uses exact same users? use user id provided other subsystem in db. not worry referential integrity between 2 system. consider following: in authenticated session, userid always valid, otherwise have serious problem in authentication process, there important thing worry about...

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -

Listeners to visualise results of load test in JMeter -