简体   繁体   English

实体框架代码优先-更新不同项目中的数据库

[英]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.

相关问题 实体框架代码首先使用不同项目中的模型 - Entity Framework Code first with models in a different project 实体框架代码第一次更新 - 数据库在CREATE DATABASE上失败 - Entity Framework code first update-database fails on CREATE DATABASE 如何为Entity Framework Code First项目设置新数据库 - How to set a new database for Entity Framework Code First project 实体框架代码优先:每个项目创建单独的数据库 - Entity Framework Code First : Create Separate Database Per Project 尝试使用Entity Framework Code First更新数据库 - Attempting to update database using Entity Framework Code First 实体框架代码优先迁移因更新数据库而失败 - Entity Framework code-first migration fails with update-database 实体框架核心更新数据库 - 无需迁移的代码优先方法 - Entity Framework Core Update Database - Code First Approach Without Migration 如何更新实体框架7迁移和数据库 - 代码优先 - How to update entity framework 7 migrations and database - Code first 在不同的数据库上运行实体框架代码优先迁移 - Run entity framework code first migration on different database 在实体框架中首先创建数据库和代码 - Creating database first and code first in Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM