好的,所以我正在使用带有 dot net core 和代码优先迁移的实体框架。这本身不是问题,我只是想知道是否有人找到了更好的方法来做到这一点。
目前我有很多实体类型配置,如下所示
public class ExampleEntityConfiguration : IEntityTypeConfiguration<ExampleEntity>
{
public void Configure(EntityTypeBuilder<ExampleEntity> builder)
{
builder.Property(p => p.Id).ValueGeneratedNever();
// more options here
}
}
我像这样在我的 dbcontext 中注册它们
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new ExampleEntityConfiguration());
// lot's more configurations here
}
有没有人遇到或知道一种方法来注册所有IEntityTypeConfiguration
接口?
这似乎是很多重复的代码,可以通过获取配置列表、循环它们并在上下文中应用来解决。我只是不知道从哪里开始获取列表IEntityTypeConfiguration
存在于特定名称空间中的类。
任何帮助/建议都会很棒。
使用 EF Core 2.2+,它变得更简单:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
Assembly assemblyWithConfigurations = GetType().Assembly; //get whatever assembly you want
modelBuilder.ApplyConfigurationsFromAssembly(assemblyWithConfigurations);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)