.NET核心实体框架-在类库中为上下文添加迁移 [英] .NET Core Entity Framework - Add migration for Context in class library

查看:117
本文介绍了.NET核心实体框架-在类库中为上下文添加迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在向.NET Core类库中的Entity Framework数据库上下文中添加初始迁移时遇到问题。



运行时:

  dotnet ef迁移添加migrationName -c PlaceholderContext 

我得到错误:

 无法在启动项目 Placeholder.Data上调用此命令。此版本的Entity Framework Core .NET命令行工具不支持ASP.NET Core和.NET Core应用程序中的类库项目上的命令。有关详细信息和解决方法,请参见http://go.microsoft.com/fwlink/?LinkId=798221。 

所以我点击了



Placeholder.Web => Startup.cs

  public void ConfigureServices(IServiceCollection服务)
{
//添加框架服务。
services.AddApplicationInsightsTelemetry(Configuration);

services.AddMvc();

//在此处注册数据库上下文,(业务层中的静态类)
services.InjectBusinessContext(@ Data Source =(localdb)\ProjectsV13; Initial Catalog = Placeholder; Integrated Security = True;连接超时= 30;);

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

我如何克服这个令人讨厌的问题?



更新(1)



我已经将Placeholder.Data类库转换为带有静态主要方法。因为我不能再引用Placeholder.Business的Placeholder.Data,所以我不得不使用Microsoft文档页面上列出的解决方法2。现在,当我运行迁移脚本时,将得到以下信息:


尚未为此DbContext配置数据库提供程序。可以通过重写DbContext.OnConfiguring
方法或通过在应用程序服务提供程序上使用AddDbContext来配置
提供程序。
如果使用AddDbContext,则还应确保DbContext类型
在其构造函数中接受
DbContextOptions对象,并将其传递给DbContext的基本构造函数


这当然行不通,dbcontext是从我的Placeholder.Web应用程序(通过业务层)注册的。然后,我唯一的选择是在新的静态main方法中添加新上下文,而我真的不想这样做。.

解决方案

您无需将数据项目转换为应用程序。这是具有类似结构的测试应用程序:





在在Data项目中的project.json中,添加asp.net核心nuget包。





现在,要创建迁移,只需右键单击数据项目,选择在文件浏览器中打开文件夹,然后在文件资源管理器中,按Shift +右键单击并单击在此处打开命令窗口。



要创建迁移,只需将启动项目指定为Web应用程序(存在startup.cs的网站)

  dotnet ef --startup-project ../TestPatterns2.Web迁移添加第二个



瞧,迁移:





要将迁移文件夹添加到数据项目中:
定义服务时,添加迁移像这样

  services.AddDbContext< ApplicationDbContext>(options => 
选项。UseSqlServer(Configuration.GetConnectionString( DefaultConnection),b => b.MigrationsAssembly( TestPatterns2.Data))));


I'm having problems adding an initial migration to my Entity Framework database context inside a .NET Core class library.

When I run:

dotnet ef migrations add migrationName -c PlaceholderContext

I get error:

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.

So I clicked on the link and learned that it's not possible to add a migration to a class library. You can however convert the class library project into an "app" project but by doing so I can not reference this "app" project from my business layer (class library).

Project structure:

Placeholder.Web (WebAPI) => Placeholder.Business (class library) => Placeholder.Data (class library)

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();
    }

How can I overcome this really annoying problem?

Update (1)

I have converted my Placeholder.Data class library to an "app" with a static main method. Because I can no longer reference Placeholder.Data from Placeholder.Business I have to go with workaround 2 listed on the microsoft doc page. When I now run the migration script I get the following:

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

Doh ofcourse this wont work, the dbcontext is registered from my Placeholder.Web app, (through business layer). Then my only option is to add a new context in the new static main method and I dont really want to do this..

解决方案

You don't need to 'convert' your data project to an app. Here is a test app with a similar structure:

In the project.json in the Data project, add the asp.net core nuget packages.

Now, to create a migration, just right click on the Data project, choose 'Open folder in file explorer,' then in file explorer, press Shift + right click and 'Open command window here.'

To create the migration, just specify the 'startup project' as the web app (where the startup.cs exists)

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

And voila, migration:

TO ADD THE MIGRATION FOLDER INTO THE DATA PROJECT: when you defined the service, add the migration point like so

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

这篇关于.NET核心实体框架-在类库中为上下文添加迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆