How to scaffold DbContext with plural DbSet property names in Entity Framework Core?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 17:43:31

问题


I use Scaffold-DbContext command in Package Manager Console to create and re-create context and entities for an existed SQL Server database:

Scaffold-DbContext -provider EntityFramework.MicrosoftSqlServer -connection "my connection string"

It works perfectly except one thing: DbSet's have property names in singular form:

public partial class MyDbContext : DbContext
{
    public virtual DbSet<Request> Request { get; set; }
    public virtual DbSet<RequestHeader> RequestHeader { get; set; }
}

I prefer these names to be in plural form (Requests etc.). In addition to web search I checked command syntax:

get-Help Scaffold-DbContext -detailed

And found nothing to change this behaviour. Here is my packages.config:

<packages>
  <package id="EntityFramework.Commands" version="7.0.0-rc1-final" targetFramework="net46" />
  <package id="EntityFramework.Core" version="7.0.0-rc1-final" targetFramework="net46" />
  ...
</packages>

How to pluralize DbSet names when scaffolding?

UPDATE 2017-04: DB First scaffolding pluralization is now possible in Entity Framework Core 1.1. Read my answer below for details.


回答1:


Pluralization is not supported in EF7 as of RC1. This and other limitations of EF7 scaffolding are being tracked here: https://github.com/aspnet/EntityFramework/issues/4038




回答2:


Pluralization is possible in EF Core 1.1. As Rowan Miller described in its blog, you need to install the Inflector and implement IDesignTimeServices to control pluralization when scaffolding. However, be aware of it:

We put these services into *.internal namespaces and reserve the right to break the APIs at any point.

So this is why a full code example is not copied to here. This answer is not accepted for the same reason - I prefer to wait until we get a stable API.

Another issue you should consider - this solution is provider-dependent. It works fine with SQL Server (I tested it). Another DBMS provider may not support this API yet. For example, latest Npgsql.EntityFrameworkCore.PostgreSQL 1.1.0 fails on scaffold when custom IDesignTimeServices is used.



来源:https://stackoverflow.com/questions/34543382/how-to-scaffold-dbcontext-with-plural-dbset-property-names-in-entity-framework-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!