【问题标题】:How to access interface in IEntityTypeConfiguration如何访问 IEntityTypeConfiguration 中的接口
【发布时间】:2022-07-12 16:12:14
【问题描述】:

我在 ef core 6 中使用代码优先。我正在使用 fluent api 配置我的实体。每个实体都有一个继承自IEntityTypeconfiguration 的配置类。我使用 ApplyConfigurationsFromAssembly 而不是 ApplyConfiguration<> 因为有很多实体。 问题是您无法在配置类中解析或注入自定义接口。

上下文:

 protected override void OnModelCreating(ModelBuilder modelBuilder)
 {
     base.OnModelCreating(modelBuilder);
     modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
 }

如果我进行构造函数注入,则不会在添加迁移中触发

public class FooConfiguration : IEntityTypeConfiguration<Foo>
 {
    public IFooDomainService _service {get; set;}
    public FooConfiguration (IFooDomainService service)
    {
       _service = service
    }
    public void Configure(EntityTypeBuilder<Foo> builder)
    {
        //Not triggering when add-migration because IFooDomainService param is need
        builder.HasData(_service.Create("Foo","Bar"))
    }
}

IEntityTypeconfiguration中的接口如何使用?

【问题讨论】:

标签: c# asp.net asp.net-core entity-framework-core .net-6.0


【解决方案1】:

使用这个类而不是之前

    public class FooConfiguration:IEntityTypeConfiguration<Foo>
{
    public override void Configure(EntityTypeBuilder<Foo> builder)
    {
        builder.HasData("Foo","Bar")
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 2022-09-29
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    相关资源
    最近更新 更多