I have a test dbcontext which I use for integration tests, which have pending changes. It's in this 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 my migrations are in a seperate project: app.Web.Data. Does anyone know an ef command where I can update IntegrationTestDbContext with the migrations from the other project?
You need a 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;
// This is the assembly where your real migration are (Your DbContext not for test!)
MigrationsAssembly = Assembly.GetExecutingAssembly();
MigrationsNamespace = "AssemblyNamespace.Migrations";
}
}
}
After that you can create the migration from the code by using DbMigrator or use the NuGet console like that:
Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] [-ConnectionStringName ] []
Update-Database will create and update the used database.
Update-Database -TargetMigration YourId -ProjectName MyProject -ConfigurationTypeName MyProject.Migrations.MyConfiguration
You can pass the connection string as parameter for the update-database command if the the startup project(app.config) does not contains the connection string
This is the command what I use for .net core (.net 5)
dnx ef migrations add ClassLibraryMigration -c ClassLibraryContext -p ClassLibrary
dnx ef database update -c ClassLibaryContext
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.