dotnet core 2.0 pluralize when scaffolding?

梦想与她 提交于 2019-12-24 14:06:51

问题


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:


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



来源:https://stackoverflow.com/questions/50767482/dotnet-core-2-0-pluralize-when-scaffolding

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