c# - How to initialize (and Seed) two Entity Framework dbcontext objects? -


i have 2 dbcontexts in code:

  1. appidentitydbcontext
  2. appdatadbcontext

the first 1 (appidentitydbcontext) vanilla dbcontext when create webapi application , indicate need local authentication. creates tables aspnetusers, aspnetroles, etc. default context named applicationdbcontext renamed it.

the second dbcontext (appdatadbcontext) context intend use deal application's data (everything except aspnet.identity data).

each of these db contexts point 2 different database files (localdb right now)

my global.asax file looks code below:

protected void application_start() {     arearegistration.registerallareas();     globalconfiguration.configure(webapiconfig.register);     filterconfig.registerglobalfilters(globalfilters.filters);     routeconfig.registerroutes(routetable.routes);     bundleconfig.registerbundles(bundletable.bundles);      // initialize user database     database.setinitializer(new initializeappidentitydbcontext());     appidentitydbcontext identitycontext = new appidentitydbcontext();     identitycontext.database.initialize(true);      // initialize data domain database sample data     database.setinitializer(new initializeappdatadbcontext());     appdatadbcontext datacontext = new appdatadbcontext();     datacontext.database.initialize(true); } 

initializeappidentitydbcontext seeds/creates --through use of usermanager, several applicationuser instances. these users populated in aspnetusers table.

similarly trying seed appdatadbcontext , have code there that.

the problem error when line below attempts execute last line:

datacontext.database.initialize(true); 

the error says:

server error in '/' application.  1 or more validation errors detected during model generation:  sampleapi.dataaccess.identityuserlogin: : entitytype 'identityuserlogin' has no key defined. define key entitytype. sampleapi.dataaccess.identityuserrole: : entitytype 'identityuserrole' has no key defined. define key entitytype. identityuserlogins: entitytype: entityset 'identityuserlogins' based on type 'identityuserlogin' has no keys defined. identityuserroles: entitytype: entityset 'identityuserroles' based on type 'identityuserrole' has no keys defined. 

this puzzling me since second db context (appdatadbcontext) not have models reference of aspnetidentity stuff being mentioned (identityuserrole, identityuserlogins, etc.). models there own classes application.

anyhow, sure violating in how i'm going initializing these 2 dbcontexts have no idea proper way accomplish it. if here, thank bearing me , reading message. ideas of should try?

many thanks,

-alex


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -