aleczandru
aleczandru

Reputation: 5449

Executin a migration for a dbContext that is not in startup project

Hi I just created a new project using ASP.NET Core 1.0 and Entity Framework Core 1.0.This is my project structure:

enter image description here

I wish to avoid adding a referenece to DataAccess layer because all calls must go threw manager layer in order to keep things decoupled and I consider that the WebApi layer should not have any knowledge of what DataAccess technology I am using. If there is any way to not add a reference to Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final" please let me know I tryed adding it in the Commercify.DataAccess.Catalog but received an error that stated is not supported class in libraries.

I have created in Commercify.DataAccess.Catalog the following dbContext:

  public class CatalogDbContext: DbContext
{
    public CatalogDbContext(DbContextOptions<CatalogDbContext> options) 
        : base(options)
    { }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"Server=(local)\SQLEXPRESS;Database=Commercify;Trusted_Connection=True;");
    }

    public DbSet<CategoryEntity> Categories { get; set; }
}

Now I want to be able to add a migration to that end I installed: in Commercify.WebApi the following assemblies:

   "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0",
      "type": "platform"
    },
     ...
    "Microsoft.EntityFrameworkCore.Design": "1.0.0-preview2-final"
  },
  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
  },

What I want now is to add a migration and in order to do that I executed the following:

dotnet ef migrations add Init

And I got the following error:

No DbContext was found in assembly 'Commercify.WebApi'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.

I remembered that back when dnx was used you could add a -p command and point ef to the project where your dbContext exists so I tryed executing this

dotnet ef migrations add Init -p Commercify.DataAccess.Catalog

And I got the following error:

Unrecognized option '-p'

How can I execute a migration tools for a dbContext that is not in startup project?

Upvotes: 1

Views: 6072

Answers (1)

EricksonG
EricksonG

Reputation: 472

AS far as I know, the only way to do this at RTM is to run the migrations from the project that contains the context. This article outlines the process of making it work. Hopefully Microsoft will fix this soon.

http://benjii.me/2016/06/entity-framework-core-migrations-for-class-library-projects/

Upvotes: 2

Related Questions