Entity Framework 6 - Code First: table schema from classes' namespace

后端 未结 3 1072
死守一世寂寞
死守一世寂寞 2021-02-13 19:00

Does any know if one can set the table schema of code first classes based on the classes\' namespace?

For example, every class in namespace Core.Foo would

3条回答
  •  情歌与酒
    2021-02-13 19:22

    I will add one thing to what octavioccl provided. If you would like to preserve table name pluralization, you can use built-in pluralization service like this:

    using System.Data.Entity.Infrastructure.DependencyResolution;
    public class CustomSchemaConvention : Convention
    {
        public CustomSchemaConvention()
        {
            var pluralizationService = DbConfiguration.DependencyResolver.GetService();
            Types().Configure(c => c.ToTable(
                pluralizationService.Pluralize(c.ClrType.Name),
                c.ClrType.Namespace.Substring(c.ClrType.Namespace.LastIndexOf('.') + 1))
            );
        }
    }
    

提交回复
热议问题