目标项目与迁移程序集不匹配

时间:2018-06-11 20:22:16

标签: asp.net-core entity-framework-core ef-migrations

我有一个多项目解决方案,我无法从主项目SELECT t1.vendor, t1.SID, COALESCE(t2I.address, t2Null.address) as address FROM table1 t1 LEFT OUTER JOIN table2 t2I ON t1.vendor = t2I.vendor AND t2I.status = 'I' LEFT OUTER JOIN table2 t2Null ON t1.vendor = t2Null.vendor AND t2Null.status IS NULL; 成功添加Db迁移。 Db上下文在程序集University.Application中定义。

在University.Application项目目录中尝试University.Infrastructure时,收到以下错误:

dotnet ef migrations add Initial

我在Startup.cs的主项目中定义了迁移程序集:

Your target project 'University.Application' doesn't match your migrations assembly 'University.Infrastructure'. 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("University.Application")). 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.

在我的University.Infrastructure项目中,我定义了一个UniversityDbContextDesignFactory(我使用两个不同的连接字符串进行运行时和设计):

services.AddEntityFrameworkSqlServer()
                .AddDbContext<UniversityContext>(options => 
                {
                    options.UseSqlServer(configuration["ConnectionString"],
                    sqlServerOptionsAction: sqlOptions => 
                    {
                        sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
                        ...

我在这里缺少什么?我没试过就试着遵循官方文档。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,但解决方法却大不相同。我的ConfigureServices与您的不同:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContextPool<AppDbContext>(option =>
        { 
           option.UseSqlServer(Configuration.GetConnectionString("ConnectionStringName")); //I set this on appsettings.json
        });
    }

这是我的appsettings.json,以防万一,这只是一个例子:

    {
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "Message": "Hello World from AppSetting.JSON",
  "ConnectionStrings": {
    "ConnectionStringName": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=DBNameHere;Integrated Security=True;"
  }
}

问题是我在解决方案目录上执行以下代码,该目录显然不包含EF Core文件,并且不在需要填充迁移文件(Data Access Layer folder)的目录上。您的情况可能有所不同,但请确保更改DbContext文件所在的文件夹。

因此,我在命令提示符下将目录更改为Data Access Layer文件夹,这是我执行以下代码的时间,因为我的启动项目位于Core.Web文件夹中,并且这也是包含以下内容的文件夹我的Startup.cs文件。 (-s means startup

dotnet ef migrations add initialcreate -s ..\Core.Web\Core.Web.csproj