EntityFramework 7 (EF7) Migrations. DbContext and StartUp Project are in different assemblies

asp.net-core-mvc entity-framework-core

Question

I'm trying to use migrations in EF7 with entityframework.commands. But my DbContext is in different assembly with Start-up project (asp.net mvc is an start-up project and Core.Implementation has a DbContex).

dnx . ef migration add MyMigration -c MyContext

System.InvalidOperationException: No DbContext named 'MyContext' was found.

I've tried to use namespace to point to other assembly but it didn't work either. Is it possible at all? Or I just have to put my context in assembly where ef7 command is?

Accepted Answer

Per issues #639, #2256, #2293, #2294, #2357, #2553 & #2748, we have a bit of work to do in that area. :-)


Popular Answer

EDIT

Since 1.0.0-rc1-final (maybe earlier) this workaroud is unnecessary

  • Now you don't need App.DataAccess/Startup.cs (just delete it if you used the workaround below)
  • You create/execure migrations form your main project (in this case App.Web)
  • However you have to specify the project (-p paramater) containing migrations:

cd App.Web
dnx ef migrations add NewMigration -p App.DataAccess

If you have multiple database contexts you also have to specify which one to use (-c parameter)

cd App.Web
dnx ef migrations add NewMigration -p App.DataAccess -c YourDbContext

END OF EDIT

I found out a workaround for that

Suppose you have 2 projects: App.Web and App.DataAccess

You can add a very basic Startup class to your App.DataAccess:

>App.Web
-->Startup.cs // your main startup
>App.DataAccess
-->path/to/ApplicationDbContext.cs
-->different/path/to/YourModels.cs
-->Startup.cs // add a simple startup class
-->project.json

The simple Startup class (App.DataAccess\Startup.cs):

using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Data.Entity;
using Microsoft.Dnx.Runtime;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;

namespace App.DataAccess
{
    public class Startup
    {
        public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
        {
            var builder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
                .AddJsonFile("../App.Web/config.json"); // path to your original configuration in Web project

            Configuration = builder.Build();
        }

        public IConfiguration Configuration { get; set; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<ApplicationDbContext>(options =>
                    options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
        }
    }
}

Than modify your project.json in App.DataAccess (App.DataAccess/project.json):

{
  "version": "1.0.0-*",
  "description": "App.DataAccess Class Library",
  "authors": [ "Author" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",
  "frameworks": {
    "dnx451": { }
  },

  "dependencies": {
    "EntityFramework.Commands": "7.0.0-beta7",
    "Microsoft.Framework.Configuration.Json": "1.0.0-beta7",
  },

  "commands": {
    "ef": "EntityFramework.Commands" // this line is important, it will give you access to 'dnx ef' in command line
  }
}

Tha all you have to do is go to App.DataAccess and use dnx ef:

cd App.DataAccess
dnx ef migrations add NewMigration
dnx ef migrations remove
dnx ef database update
dnx ef ...



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why