在 dbcontext 中用动态值填充 modelBuilder.Entity
Fill modelBuilder.Entity with dynamic values in dbcontext
我有很多观点,我必须使用(在OnModelCreating asp.net核心5)
modelBuilder.Entity <y1> (). HasNoKey (). ToView (null);
...
modelBuilder.Entity <yn> (). HasNoKey (). ToView (null);
有没有办法动态设置实体值?
modelBuilder.Entity <MyClass1> (). HasNoKey (). ToView (null);
...
modelBuilder.Entity <MyClassn> (). HasNoKey (). ToView (null);
我尝试了几种方法但出现以下错误 =>y1 是类型但像变量一样使用
get items => foreach (var entity in modelBuilder.Model.GetRootEntityTypes())
我想您正在使用 EF 5.0,您是否尝试过使用 Shared-Type 实体类型?
EF Core 5.0 允许将同一个 CLR 类型映射到多个不同的实体类型;这种类型被称为共享类型实体类型。虽然任何 CLR 类型都可以与此功能一起使用,但 .NET 字典提供了一个特别引人注目的用例,我们称之为“属性 包”:
public class ProductsContext : DbContext
{
public DbSet<Dictionary<string, object>> Products => Set<Dictionary<string, object>>("Product");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.SharedTypeEntity<Dictionary<string, object>>("Product", b =>
{
b.IndexerProperty<int>("Id");
b.IndexerProperty<string>("Name").IsRequired();
b.IndexerProperty<decimal>("Price");
});
}
}
更新:
TEntity 是 class 而不是类型。
我还没有测试过,但也许这可以说明一些问题:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
foreach (var eType in modelBuilder.Model.GetEntityTypes())
{
EntityTypeBuilder x = modelBuilder.SharedTypeEntity<Dictionary<string, object>>(eType.Name, b =>
{
foreach (var p in eType.GetProperties())
{
if(p.GetType() == typeof(int))
{
b.IndexerProperty<int>(p.Name);
}
else if(p.GetType() == typeof(string))
{
b.IndexerProperty<string>(p.Name);
}
}
}).SharedTypeEntity(eType.Name, eType.GetType());
x.HasNoKey();
}
}
foreach (var entity in modelBuilder.Model.GetEntityTypes())
{
modelBuilder.Entity(entity.ClrType).HasNoKey().ToView(null);
}
modelBuilder.Entity <y1> (). HasNoKey (). ToView (null);
...
modelBuilder.Entity <yn> (). HasNoKey (). ToView (null);
有没有办法动态设置实体值?
modelBuilder.Entity <MyClass1> (). HasNoKey (). ToView (null);
...
modelBuilder.Entity <MyClassn> (). HasNoKey (). ToView (null);
我尝试了几种方法但出现以下错误 =>y1 是类型但像变量一样使用
get items => foreach (var entity in modelBuilder.Model.GetRootEntityTypes())
我想您正在使用 EF 5.0,您是否尝试过使用 Shared-Type 实体类型?
EF Core 5.0 允许将同一个 CLR 类型映射到多个不同的实体类型;这种类型被称为共享类型实体类型。虽然任何 CLR 类型都可以与此功能一起使用,但 .NET 字典提供了一个特别引人注目的用例,我们称之为“属性 包”:
public class ProductsContext : DbContext
{
public DbSet<Dictionary<string, object>> Products => Set<Dictionary<string, object>>("Product");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.SharedTypeEntity<Dictionary<string, object>>("Product", b =>
{
b.IndexerProperty<int>("Id");
b.IndexerProperty<string>("Name").IsRequired();
b.IndexerProperty<decimal>("Price");
});
}
}
更新: TEntity 是 class 而不是类型。 我还没有测试过,但也许这可以说明一些问题:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
foreach (var eType in modelBuilder.Model.GetEntityTypes())
{
EntityTypeBuilder x = modelBuilder.SharedTypeEntity<Dictionary<string, object>>(eType.Name, b =>
{
foreach (var p in eType.GetProperties())
{
if(p.GetType() == typeof(int))
{
b.IndexerProperty<int>(p.Name);
}
else if(p.GetType() == typeof(string))
{
b.IndexerProperty<string>(p.Name);
}
}
}).SharedTypeEntity(eType.Name, eType.GetType());
x.HasNoKey();
}
}
foreach (var entity in modelBuilder.Model.GetEntityTypes())
{
modelBuilder.Entity(entity.ClrType).HasNoKey().ToView(null);
}