Philip Walter
Philip Walter

Reputation: 21

EF Core Migration Failing

I am new to Code First building with EF Core and need a little assistance. I have a solution with 3 projects: DataProvider, DataModel, UserInterface. I am roughly following the example in Jon P Smith's book Entity Framework Core in Action.

DataModel holds my ef entities (class definitions). DataProvider holds my DbContext. UserInterface is the startup web application.

I have the following in 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) :

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 :

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 :

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

If someone can help me get over this initial Code First hump, I can handle the rest.

Upvotes: 1

Views: 165

Answers (1)

Philip Walter
Philip Walter

Reputation: 21

My problem was correctly diagnosed by Jeremy Lakeman in the comments above. Using NuGet, I brought EntityFrameworkCore.Tools and EntityFrameworkCore.SqlServer 3.x into a .NET Core 2.x project. I removed those references and added EF versions to match my .NET Core version, and the Add-Migration command worked successfully. Thanks to Jeremy and ESG for helping me out!

Upvotes: 1

Related Questions