2017-02-13 130 views
9

我在添加一个初始迁移到.NET核心类库里面我的实体框架数据库上下文问题的迁移。.NET核心实体框架 - 上下文添加在类库

当我运行:

dotnet ef migrations add migrationName -c PlaceholderContext 

我得到错误:

Could not invoke this command on the startup project 'Placeholder.Data'. This version of the Entity Framework Core .NET Command Line Tools does not support commands on class library projects in ASP.NET Core and .NET Core applications. See http://go.microsoft.com/fwlink/?LinkId=798221 for details and workarounds. 

于是我点击了link,得知这是不可能的迁移添加到类库。但是,您可以将类库项目转换为“应用程序”项目,但通过这样做我无法从业务层(类库)中引用此“应用程序”项目。

项目结构:

Placeholder.Web(的WebAPI)=>Placeholder.Business(类库)=>Placeholder.Data(类库)

Project structure

Placeholder.Web => Startup.cs

public void ConfigureServices(IServiceCollection services) 
    { 
     // Add framework services. 
     services.AddApplicationInsightsTelemetry(Configuration); 

     services.AddMvc(); 

     //HERE WE REGISTER DB CONTEXT, (STATIC CLASS IN BUSINESS LAYER) 
     services.InjectBusinessContext(@"Data Source=(localdb)\ProjectsV13;Initial Catalog=Placeholder;Integrated Security=True;Connect Timeout=30;"); 

     services.InjectWebServices(); 
     services.InjectBusinessServices(); 
    } 

我怎样才能克服这个真的很烦人的问题?

更新(1)

我已经转换我Placeholder.Data类库到“应用程序”有静态主要方法。因为我无法再引用Placeholder.Business中的Placeholder.Data,所以我必须使用microsoft doc页面上列出的解决方法2。当我现在运行迁移脚本我得到如下:

No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext

卫生署ofcourse这不会工作,在的DbContext是从我Placeholder.Web应用程序注册(通过业务层)。然后我唯一的选择就是在新的静态main方法添加新的上下文,我真的不希望这样做..

回答

6

你并不需要“转换”你的数据项目的应用程序。下面是一个类似的结构的测试程序:

project structure

在数据项目project.json,添加asp.net核心的NuGet包。

project.json

现在,要创建一个迁移,仅仅在数据项目点击右键,选择“在文件浏览器打开文件夹”,然后在文件浏览器,按Shift +右键点击“打开命令窗口在这里。 “

若要迁移,只需指定 '启动项目' 为Web应用程序(其中startup.cs存在)

dotnet ef --startup-project ../TestPatterns2.Web migrations add Second 

migrations

瞧,迁移:

second migration

将迁移文件夹添加到数据项目中: 当您定义服务ce,像这样添加迁移点

services.AddDbContext<ApplicationDbContext>(options => 
      options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), b => b.MigrationsAssembly("TestPatterns2.Data"))); 
+0

我以后会在我回家时试试这个! – Reft

+0

如果它适合你,请标记为答案。如果您想更进一步,您还可以在DATA类库项目中扩展IServiceCollection,并在其中添加DI和连接字符串,以便您的Web项目完全独立于DATA项目。 – Reza

+0

对不起,延迟回复,只是尝试了你的代码,它的工作原理,欢呼声。但!如果你看看我的项目结构,我有一个名为migrations的自创文件夹。当我运行命令时,我在根目录中得到一个新的迁移文件夹?我能改变这个吗? 2.如果我想在第一次初始迁移后添加更多迁移,我是否需要再次执行这些步骤,或者可以使用包管理器控制台以正常方式创建它? – Reft

1

它只需要为使迁移的应用程序(需要一个入口点),以便使后图书馆的一个应用程序,并创建您的迁移,注释掉您的project.json中的buildOptionsruntimes元素。现在它将再次建成一个图书馆。

取消其注释,每当你需要添加另一个迁移。

+0

我试过从“netstandard1.6”转换为“netcoreapp1.0”,但我得到的.Data与netstandard1.6不兼容。 – Reft

+0

好吧,我不得不卸载业务层,(参考),然后它的工作。我将类库转换为一个应用程序。因为我无法从.Business中引用.Data。我不得不使用Workaround 2。似乎我需要使用main方法在.Data中注册上下文?我不想这样做.. – Reft

+0

@Reft我使用的解决方案是为它声明一个DBFactory。我不记得我发现要做的博客文章,对不起。 – BradleyDotNET