使用其他程序集添加迁移 [英] Add migration with different assembly

查看:144
本文介绍了使用其他程序集添加迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET CORE 1.0.0开发一个项目,并且正在使用EntityFrameworkCore.我有单独的程序集,我的项目结构如下所示:

I am working on a project with ASP.NET CORE 1.0.0 and I am using EntityFrameworkCore. I have separate assemblies and my project structure looks like this:

ProjectSolution
   -src
      -1 Domain
         -Project.Data
      -2 Api
         -Project.Api

在我的Project.Api中是Startup

public void ConfigureServices(IServiceCollection services)
    {            
        services.AddDbContext<ProjectDbContext>();

        services.AddIdentity<IdentityUser, IdentityRole>()
                .AddEntityFrameworkStores<ProjectDbContext>()
                .AddDefaultTokenProviders();
    }

DbContext在我的Project.Data项目中

public class ProjectDbContext : IdentityDbContext<IdentityUser>
{
    public ProjectDbContext(DbContextOptions<ProjectDbContext> options) : base(options)
    {

    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {

        var builder = new ConfigurationBuilder();
        builder.SetBasePath(Directory.GetCurrentDirectory());
        builder.AddJsonFile("appsettings.json");
        IConfiguration Configuration = builder.Build();

        optionsBuilder.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection"));
        base.OnConfiguring(optionsBuilder);
    }
}

当我尝试进行初始迁移时,出现此错误:

When I try to make the initial migration, I get this error:

您的目标项目'Project.Api'与迁移程序集'Project.Data'不匹配.请更改目标项目或更改迁移程序集. 通过使用DbContextOptionsBuilder更改迁移程序集.例如. options.UseSqlServer(connection,b => b.MigrationsAssembly("Project.Api")).默认情况下,迁移程序集是包含DbContext的程序集. 通过使用程序包管理器控制台的默认项目"下拉列表,或从包含迁移项目的目录中执行"dotnet ef",将您的目标项目更改为迁移项目."

"Your target project 'Project.Api' doesn't match your migrations assembly 'Project.Data'. Either change your target project or change your migrations assembly. Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("Project.Api")). By default, the migrations assembly is the assembly containing the DbContext. Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project."

看到此错误后,我尝试执行位于Project.Api中的此命令:

After I seeing this error, I tried to execute this command located in Project.Api:

dotnet ef --startup-project ../Project.Api --assembly"../../1 Data/Project.Data"迁移添加了初始

dotnet ef --startup-project ../Project.Api --assembly "../../1 Data/Project.Data" migrations add Initial

我收到此错误:

选项'assembly'的意外值'../../1 Domain/Project.Data'"

"Unexpected value '../../1 Domain/Project.Data' for option 'assembly'"

当我尝试使用'-assembly'参数执行命令时,我不知道为什么会出现此错误.

I don't know why I get this error, when I try to execute the command with the '-assembly' parameter.

我无法从其他程序集中创建初始迁移",我已经搜索了有关该信息的信息,但未得到任何结果.

I can't create a Initial Migration from other assembly and I've searched for information about it but didn't got any results.

有人有类似的问题吗?

推荐答案

所有EF命令都具有 targetAssembly =您正在操作的目标项目.在命令行上,它是当前工作目录中的项目.在Package Manager控制台中,无论是在该窗格右上角的下拉框中选择的项目,都是什么.

targetAssembly = the target project you are operating on. On the command line, it is the project in the current working directory. In Package Manager Console, it is whatever project is selected in the drop down box on the top right of that window pane.

migrationsAssembly =包含迁移代码的程序集.这是可配置的.默认情况下,它将是包含DbContext的程序集(在您的情况下为Project.Data.dll). 如错误消息所示,您有两种选择来解决此问题

migrationsAssembly = assembly containing code for migrations. This is configurable. By default, this will be the assembly containing the DbContext, in your case, Project.Data.dll. As the error message suggests, you have have a two options to resolve this

1-更改目标部件.

cd Project.Data/
dotnet ef --startup-project ../Project.Api/ migrations add Initial

// code doesn't use .MigrationsAssembly...just rely on the default
options.UseSqlServer(connection)

2-更改迁移程序集.

2 - Change the migrations assembly.

cd Project.Api/
dotnet ef migrations add Initial

// change the default migrations assembly
options.UseSqlServer(connection, b => b.MigrationsAssembly("Project.Api"))

这篇关于使用其他程序集添加迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆