EntityFramework 7(EF7)迁移.DbContext和StartUp Project位于不同的程序集中

shk*_*per 19 entity-framework-core asp.net-core-mvc

我正在尝试使用entityframework.commands在EF7中使用迁移.但我的DbContext与启动项目处于不同的程序集(asp.net mvc是一个启动项目,Core.Implementation有一个DbContex).

dnx.ef migration添加MyMigration -c MyContext

System.InvalidOperationException:未找到名为"MyContext"的DbContext.

我试图使用命名空间指向其他程序集,但它也没有用.有可能吗?或者我只需要将我的上下文放在ef7命令所在的汇编中?

And*_*Gis 24

编辑

从1.0.0-rc1-final(可能更早)开始,这个工作区是不必要的

  • 现在你不需要App.DataAccess/Startup.cs(如果你使用下面的解决方法,只需删除它)
  • 您从主项目创建/执行迁移(在本例中 App.Web)
  • 但是,您必须指定-p包含迁移的项目(参数):

cd App.Web
dnx ef migrations add NewMigration -p App.DataAccess
Run Code Online (Sandbox Code Playgroud)

如果您有多个数据库上下文,则还必须指定要使用的数据库上下文(-c参数)

cd App.Web
dnx ef migrations add NewMigration -p App.DataAccess -c YourDbContext
Run Code Online (Sandbox Code Playgroud)

编辑结束

我发现了一个解决方法

假设你有2个项目:App.WebApp.DataAccess

您可以为您的App.DataAccess以下内容添加一个非常基本的Startup类:

>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
Run Code Online (Sandbox Code Playgroud)

简单的Startup类(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)
        {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

比在App.DataAccess(App.DataAccess/project.json)中修改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
  }
}
Run Code Online (Sandbox Code Playgroud)

你需要做的就是去App.DataAccess使用dnx ef:

cd App.DataAccess
dnx ef migrations add NewMigration
dnx ef migrations remove
dnx ef database update
dnx ef ...
Run Code Online (Sandbox Code Playgroud)


bri*_*lam 10

根据问题#639,#2256,#2293,#2294,#2357,#2553#2748,我们在该领域有一些工作要做.:-)

  • @neodymium请参阅[2015年10月28日的设计会议记录](https://github.com/aspnet/EntityFramework/wiki/Design-Meeting-Notes-(2015年10月28日)) (2认同)