小编Wue*_*ueF的帖子

如何在IDesignTimeServices中正确使用IPluralizer

在我的应用程序中,我使用代码优先方法,并且仅通过将实体添加到DbContext中IEntityTypeConfiguration<>。我的目标是实现多元化的表名,即Models for Model

在阅读了文档文章之后, 我的疑问是我的复数器应该被注册并IPluralizer在创建迁移时使用,但是不是。

当然,我可以隐式使用DbSet<Model> Models {get;set;}或use builder.ToTable("Models");,但是在我的通用方案中,我想避免这种情况,尤其是因为我希望某些模型不要覆盖抽象的通用方案。

问题是,我是在做错什么,还是我误解了它的行为方式

AppDesignService.cs

public class AppDesignService : IDesignTimeServices
{
    public void ConfigureDesignTimeServices(IServiceCollection services)
    {
        Debugger.Launch();
        services.AddSingleton<IPluralizer, InflectorPluralizer>();
    }
}
Run Code Online (Sandbox Code Playgroud)

MyDbContext.cs

public class AppDbContext : IdentityDbContext<AppUser,AppRole,Guid>
{
    public EmsDbContext(DbContextOptions options) : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.ApplyConfigurationsFromAssembly(Assembly.GetAssembly(typeof(AppDbContext)));
    }
}
Run Code Online (Sandbox Code Playgroud)

启动文件

public void ConfigureServices(IServiceCollection services)
{
    var connectionString = Configuration.GetConnectionString("DefaultConnection");

    services.AddDbContext<DbContext, AppDbContext>(opt =>
     { …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework-core asp.net-core

2
推荐指数
1
解决办法
286
查看次数

标签 统计

asp.net-core ×1

c# ×1

entity-framework-core ×1