c# - Entity-Framework auto update -


i try implement entity-framework project! project plugin-based not know object have save database.

i have implemented so:

public class databasecontext : dbcontext {     public databasecontext() : base()     {         database.initialize(true);     }      protected override void onmodelcreating(dbmodelbuilder modelbuilder)     {         foreach( plugindto plugin in backendcontext.current.pluginmanager._plugins) {             foreach(type obj in plugin.plugin.getplugindatabaseobjects())             {                 type typ = typeof(entitytypeconfiguration<>).makegenerictype(obj);                  list<methodinfo> l = modelbuilder.gettype().getmethods().tolist<methodinfo>();                  methodinfo m_entitiy = modelbuilder.gettype().getmethod("entity").makegenericmethod(new type[] { obj });                 var configobj = m_entitiy.invoke(modelbuilder, null);                  methodinfo m_totable = configobj.gettype().getmethod("totable", new type[] { typeof(string) });                 m_totable.invoke(configobj, new object [] { obj.name });              }         }          base.onmodelcreating(modelbuilder);     }  } 

but exception, when give change:

the model backing 'databasecontext' context has changed since database created. consider using code first migrations update database (http://go.microsoft.com/fwlink/?linkid=238269).

this error completly logical. database out of sync, how update? have read this:

 var config = new dbmigrationsconfiguration<mycontext> { automaticmigrationsenabled = true };  var migrator = new dbmigrator(config);  migrator.update(); 

but don't know how , use correctly! thank much!

edit1: when try to: enable-migrations –enableautomaticmigrations

i got error:

system.nullreferenceexception: object reference not set instance of object.    @ som.backend.database.databasecontext.onmodelcreating(dbmodelbuilder modelbuilder) in c:\users\flo\documents\visual studio 2015\projects\som\backend\backendservice\backendservice\database\databasecontext.cs:line 26    @ system.data.entity.internal.lazyinternalcontext.createmodelbuilder()    @ system.data.entity.internal.lazyinternalcontext.createmodel(lazyinternalcontext internalcontext)    @ system.data.entity.internal.retrylazy`2.getvalue(tinput input)    @ system.data.entity.internal.lazyinternalcontext.initializecontext()    @ system.data.entity.internal.lazyinternalcontext.markdatabaseinitialized()    @ system.data.entity.database.initialize(boolean force)    @ som.backend.database.databasecontext..ctor() in c:\users\flo\documents\visual studio 2015\projects\som\backend\backendservice\backendservice\database\databasecontext.cs:line 21 --- end of stack trace previous location exception thrown ---    @ system.runtime.exceptionservices.exceptiondispatchinfo.throw()    @ system.data.entity.infrastructure.dbcontextinfo.createinstance()    @ system.data.entity.infrastructure.dbcontextinfo..ctor(type contexttype, dbproviderinfo modelproviderinfo, appconfig config, dbconnectioninfo connectioninfo, func`1 resolver)    @ system.data.entity.migrations.dbmigrator..ctor(dbmigrationsconfiguration configuration, dbcontext userscontext, databaseexistencestate existencestate, boolean calledbycreatedatabase)    @ system.data.entity.migrations.dbmigrator..ctor(dbmigrationsconfiguration configuration)    @ system.data.entity.migrations.design.migrationscaffolder..ctor(dbmigrationsconfiguration migrationsconfiguration)    @ system.data.entity.migrations.design.toolingfacade.scaffoldrunner.run()    @ system.appdomain.docallback(crossappdomaindelegate callbackdelegate)    @ system.appdomain.docallback(crossappdomaindelegate callbackdelegate)    @ system.data.entity.migrations.design.toolingfacade.run(baserunner runner)    @ system.data.entity.migrations.design.toolingfacade.scaffoldinitialcreate(string language, string rootnamespace)    @ system.data.entity.migrations.enablemigrationscommand.<>c__displayclass2.<.ctor>b__0()    @ system.data.entity.migrations.migrationsdomaincommand.execute(action command) 

edit2:

  <connectionstrings>     <add name="databasecontext" providername="system.data.sqlserverce.4.0" connectionstring="data source=som_db.sdf;max database size=1024" />   </connectionstrings> 

what asking doable, limitations.

solution:

first, remove the

database.initialize(true); 

from constructor. constructor called multiple times, including migrations.

second, create configuration class this

internal sealed class datacontextconfiguration : dbmigrationsconfiguration<datacontext> {     public datacontextconfiguration()     {         automaticmigrationsenabled = true;         automaticmigrationdatalossallowed = true;         contextkey = "datacontext";     } } 

then change constructor follows:

public datacontext() {     database.setinitializer(new migratedatabasetolatestversion<datacontext, datacontextconfiguration>()); } 

and done. database tables entity types plugins automatically created/updated accordingly.

to recap, standard code first approach automatic migrations enabled, dynamic entity type registration/configuration inside onmodelcreating override.

limitations:

  • if don't set automaticmigrationdatalossallowed = true, when existing plugin removed, ef generate exception because not allowed delete corresponding tables. , if so, plugin tables deleted, if plugin added again, start zero.

  • the plugin entities can configured using data annotations. if want give them full control, you'd need change plugin interface , instead of taking entity types, call method , pass dbmodelbuilder instead, can use fluent api configure entity types themselves.


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -