[英]Entity Framework code first - update database in different project
I have a test dbcontext which I use for integration tests, which have pending changes. 我有一个用于集成测试的dbcontext测试,它具有未决的更改。 It's in this project: app.WebApi.Integration
在此项目中: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. 但是我的迁移是在一个单独的项目中:app.Web.Data。 Does anyone know an ef command where I can update IntegrationTestDbContext with the migrations from the other project?
有谁知道ef命令,在哪里可以用来自其他项目的迁移更新IntegrationTestDbContext?
You need a custom DbMigrationsConfiguration : 您需要一个自定义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: 之后,您可以使用DbMigrator从代码创建迁移,或使用类似这样的NuGet控制台:
Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] [-ConnectionStringName ] [] 更新数据库[-SourceMigration] [-TargetMigration] [-Script] [-Force] [-ProjectName] [-StartUpProjectName] [-ConfigurationTypeName] [-ConnectionStringName] []
Update-Database will create and update the used database. Update-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 如果启动项目(app.config)不包含连接字符串,则可以将连接字符串作为update-database命令的参数传递
This is the command what I use for .net core (.net 5) 这是我用于.net core(.net 5)的命令
dnx ef migrations add ClassLibraryMigration -c ClassLibraryContext -p ClassLibrary
dnx ef database update -c ClassLibaryContext
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.