Entity Framework 4.1 Code First:获取具有特定基类的所有实体
我有一个 DbContext,它设置了不同的 DbSet
,其中所有类型都派生自同一基类:
public class Foo : Entity { }
public class Bar : Entity { }
MyDbContext : DbContext
{
public DbSet<Foo> Foos { get; set; }
public DbSet<Bar> Bars { get; set; }
}
是否可以获取具有 Entity
基的所有实体一个查询中的类,例如:
DbContext.Set<Entity>(); // doesn't work
我尝试向 DbContext 引入显式的 DbSet
,但这会导致数据库中的所有实体生成一张大表。
附加问题:如果这以某种方式起作用,那么查询接口怎么样?
编辑:
我按照 Ladislav Mrnka 的 链接 并进行了如下映射:
MyDbContext : DbContext
{
public DbSet<Entity> Entities { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Foo
modelBuilder.Entity<Foo>().Map(x =>
{
x.MapInheritedProperties();
x.ToTable("Foo");
})
.HasMany(x => x.Tags).WithMany();
modelBuilder.Entity<Foo>()
.Property(x => x.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
// Bar
// same thing for Bar and a bunch of other Entities
base.OnModelCreating(modelBuilder);
}
}
这现在抛出错误
属性“Id”不是类型“Foo”上声明的属性。使用 Ignore 方法或 NotMappedAttribute 数据注释验证该属性是否未从模型中显式排除。确保它是有效的原始属性。
我还尝试将 Key 显式设置为 Id
属性:
modelBuilder.Entity<Foo>().Map(x => {...})
.HasKey(x => x.Id)
.HasMany(x => x.Tags).WithMany();
我缺少什么?
I have a DbContext with set up different DbSet<T>
s with all types that derive from the same base class:
public class Foo : Entity { }
public class Bar : Entity { }
MyDbContext : DbContext
{
public DbSet<Foo> Foos { get; set; }
public DbSet<Bar> Bars { get; set; }
}
Is it possible to get all entities which have the Entity
base class in one query, like:
DbContext.Set<Entity>(); // doesn't work
I tried to introduce an explicit DbSet<Entity>
to the DbContext, but that results in one big table for all entities in the database.
Additional question: If this works somehow, what about querying for interfaces?
Edit:
I followed the instructions on Ladislav Mrnka's link and did my mappings like follows:
MyDbContext : DbContext
{
public DbSet<Entity> Entities { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Foo
modelBuilder.Entity<Foo>().Map(x =>
{
x.MapInheritedProperties();
x.ToTable("Foo");
})
.HasMany(x => x.Tags).WithMany();
modelBuilder.Entity<Foo>()
.Property(x => x.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
// Bar
// same thing for Bar and a bunch of other Entities
base.OnModelCreating(modelBuilder);
}
}
This throws now the error
The property 'Id' is not a declared property on type 'Foo'. Verify that the property has not been explicitly excluded from the model by using the Ignore method or NotMappedAttribute data annotation. Make sure that it is a valid primitive property.
I also tried to set the Key explicitly to the Id
property:
modelBuilder.Entity<Foo>().Map(x => {...})
.HasKey(x => x.Id)
.HasMany(x => x.Tags).WithMany();
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要引入 TPC 继承。之后,
DbContext.Set()
将起作用,并且每个实体仍然有一个表。You need to introduce TPC inheritance. After that
DbContext.Set<Entity>()
will work and you will still have table per entity.只是针对“编辑”部分中的问题:
错误消息表明您的基类
Entity
中有Id
键属性。然后,您需要在此类上配置关键属性,而不是在派生的 Foo 类上配置:Just to your problem in your Edit section:
The error message indicates that you have the
Id
key property in your base classEntity
. Then you need to configure the key property on this class and not on the derivedFoo
class: