c# - Entity Framework code first - update database in different project -
i have test dbcontext use integration tests, have pending changes. it's in project: app.webapi.integration
using system.data.entity; using system.data.entity.infrastructure; using app.web.data; using app.web.model.entities; namespace app.webapi.integration.data { public class integrationtestdbcontext : dbcontext, idbcontextfactory<integrationtestdbcontext> { public integrationtestdbcontext(string conn = "defaultconnection") : base(conn) { configuration.lazyloadingenabled = false; } public integrationtestdbcontext() { } public virtual idbset<question> questions { get; set; } public virtual idbset<answer> answers { get; set; } public virtual void markas(object item, entitystate entitystate) { entry(item).state = entitystate; } public integrationtestdbcontext create() { return new integrationtestdbcontext(); } } }
but migrations in seperate project: app.web.data. know ef command can update integrationtestdbcontext migrations other project?
you need custom dbmigrationsconfiguration :
namespace myprogram.migrations { using system; using system.data.entity; using system.data.entity.migrations; using system.linq; using system.reflection; using myprogram; internal sealed class myconfiguration : dbmigrationsconfiguration<myprogram.mydbcontext> { public configuration() { automaticmigrationsenabled = false; // assembly real migration (your dbcontext not test!) migrationsassembly = assembly.getexecutingassembly(); migrationsnamespace = "assemblynamespace.migrations"; } } }
after can create migration code using dbmigrator or use nuget console that:
update-database [-sourcemigration ] [-targetmigration ] [-script] [-force] [-projectname ] [-startupprojectname ] [-configurationtypename ] [-connectionstringname ] []
update-database create , update used database.
update-database -targetmigration yourid -projectname myproject -configurationtypename myproject.migrations.myconfiguration
you can pass connection string parameter update-database command if the startup project(app.config) not contains connection string
Comments
Post a Comment