Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
527 views
in Technique[技术] by (71.8m points)

asp.net core mvc - EntityFramework 7 (EF7) Migrations. DbContext and StartUp Project are in different assemblies

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.DataAccessStartup.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 ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...