本文介绍了dotnet core 2.0在搭建时是复数的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ef core 2设置.net core 2.x类库.已经从我的数据库模式生成了实体的支架.但是,我什至忘记检查是否存在使实体名称复数的选项.当我从使用EF 6.1的类库中提取一个方法并将实体复数时,我注意到了这一点.是否可以选择并简单地将我的实体重新生成为复数形式?

Setting my a .net core 2.x class library with ef core 2. Have generated the scaffolding for the entities from my db schema. However, I forgot to even check whether there as an option to pluralize the entity names. I noticed this when I pulled over one my methods from a class library that uses EF 6.1 and the entities are pluralized. Is there an option for this and to simply regenerate my entities as pluralized?

推荐答案

  1. 编写一个实现 Microsoft.EntityFrameworkCore.Design.IPluralizer 接口的类.您可以编写自己的文件,也可以使用NuGet程序包,例如 Inflector

  1. Write a class that implements the Microsoft.EntityFrameworkCore.Design.IPluralizer interface. You can write your own, or use a NuGet package such as Inflector

public class Pluralizer : IPluralizer
{
    public string Pluralize(string name)
    {
        return Inflector.Inflector.Pluralize(name) ?? name;
    }

    public string Singularize(string name)
    {
        return Inflector.Inflector.Singularize(name) ?? name;
    }
}

  • 编写一个实现 Microsoft.EntityFrameworkCore.Design.IDesignTimeServices 接口的类,以在实体框架项目中注册 IPluralizer 实现.

  • Write a class that implements the Microsoft.EntityFrameworkCore.Design.IDesignTimeServices interface to register your IPluralizer implementation in your entity framework project.

    public class DesignTimeServices : IDesignTimeServices
    {
        public void ConfigureDesignTimeServices(IServiceCollection services)
        {
            services.AddSingleton<IPluralizer, Pluralizer>();
        }
    }
    

  • 从包管理器控制台照常运行(或重新运行) Scaffold-DbContext 命令.如果要覆盖以前生成的代码,则需要 -force 选项.

  • Run (or rerun) your Scaffold-DbContext command from the package manager console as usual. If you want it to overwrite the previously generated code, you need the -force option.

    这篇关于dotnet core 2.0在搭建时是复数的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 09-23 17:25