简体   繁体   English

EF Core 迁移失败

[英]EF Core Migration Failing

I am new to Code First building with EF Core and need a little assistance.我是使用 EF Core 构建 Code First 的新手,需要一些帮助。 I have a solution with 3 projects: DataProvider, DataModel, UserInterface.我有一个包含 3 个项目的解决方案:DataProvider、DataModel、UserInterface。 I am roughly following the example in Jon P Smith's book Entity Framework Core in Action.我大致遵循 Jon P Smith 的书 Entity Framework Core in Action 中的示例。

DataModel holds my ef entities (class definitions). DataModel 包含我的 ef 实体(类定义)。 DataProvider holds my DbContext. DataProvider 拥有我的 DbContext。 UserInterface is the startup web application. UserInterface 是启动 web 应用程序。

I have the following in DataProvider > P1DbContext.cs:我在 DataProvider > P1DbContext.cs 中有以下内容:

namespace DataProvider
{
    public class P1DbContext : DbContext
    {

        public DbSet<ToDoItem> ToDoItems { get; set; }

        public P1DbContext(DbContextOptions<P1DbContext> options) : base(options) { }

    }
}

And the following in DataProvider > ContextFactoryForMigrations.cs (this was suggested by Jon P Smith for migrations with multiple projects in one solution):以及 DataProvider > ContextFactoryForMigrations.cs 中的以下内容(Jon P Smith 建议在一个解决方案中迁移多个项目):

class ContextFactoryForMigrations : IDesignTimeDbContextFactory<P1DbContext>
{

    private const string ConnectionString = "Server=localhost;Database=master;Trusted_Connection=True;";

    public P1DbContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<P1DbContext>();
        optionsBuilder.UseSqlServer(ConnectionString,
            b => b.MigrationsAssembly("DataProvider"));

        return new P1DbContext(optionsBuilder.Options);
    }

}

And then I have this in UserInterface > Startup.cs:然后我在 UserInterface > Startup.cs 中有这个:

public void ConfigureServices(IServiceCollection services)
        {
            string connection = "Server = localhost; Database = master; Trusted_Connection = True;";
            services.AddMvc();
            services.AddDbContext<P1DbContext>(options => options.UseSqlServer(connection,
                b => b.MigrationsAssembly("DataProvider")));
        }

Proper references are made and all projects compile.进行了适当的引用并编译了所有项目。

When I go to Package Manager Console, I run the following, with DataProvider as the default project in PM Console and UserInterface as startup project in the solution:当我从 go 到 Package 管理器控制台时,我运行以下命令,将 DataProvider 作为 PM 控制台中的默认项目,将 UserInterface 作为解决方案中的启动项目:

Add-Migration InitializeDb -Project DataProvider -StartupProject UserInterface

I get this response:我得到这个回应:

Could not load assembly ''. Ensure it is referenced by the startup project ''.

It doesn't even appear to know what assembly to look for or what the startup project is.它甚至不知道要查找什么程序集或启动项目是什么。 I'm sure I'm missing something stupid here, but thought I would ask before I beat my head against the wall too much longer.我确定我在这里遗漏了一些愚蠢的东西,但我想在我把头撞到墙上太久之前我会问一下。

Here is a link to a repo with the current state of the project: https://github.com/philipwalter/CodeFirstEFTest这是项目当前 state 的回购链接: https://github.com/philipwalter/CodeFirstEFTest

If someone can help me get over this initial Code First hump, I can handle the rest.如果有人可以帮助我克服最初的 Code First 驼峰,我可以处理 rest。

My problem was correctly diagnosed by Jeremy Lakeman in the comments above. Jeremy Lakeman 在上面的评论中正确诊断了我的问题。 Using NuGet, I brought EntityFrameworkCore.Tools and EntityFrameworkCore.SqlServer 3.x into a .NET Core 2.x project.使用 NuGet,我将 EntityFrameworkCore.Tools 和 EntityFrameworkCore.SqlServer 3.x 带入了 .NET Core 2.x 项目。 I removed those references and added EF versions to match my .NET Core version, and the Add-Migration command worked successfully.我删除了这些引用并添加了 EF 版本以匹配我的 .NET 核心版本,并且 Add-Migration 命令成功运行。 Thanks to Jeremy and ESG for helping me out!感谢 Jeremy 和 ESG 帮助我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM