告诉 EF6 不要保留基础 Class 但设置 Fluent API 包含

Tell EF6 Not to Persist Base Class but set FluentAPI contrains

我有一个名为 "Entity" 的基础 class,我在其中放置了应由任何实体(例如 Id、CreateAt、UpdateAt)继承的标准字段。我更喜欢使用 FluentAPI,因为据说它比注释更强大,并且它支持干净易读的 POCO classes。有没有一种方法可以在流利的 api 中为父实体 class 在这些字段上设置属性并继承它,但也不会在数据库中为 [=13] 生成 table =] POCO class?

正常的实体配置应该是这样的:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Planet>().HasKey(b => b.Id);
}

但是,正如您所注意到的,这也会将该类型注册为模型的一部分。 Entity Framework 6 虽然根据文档介绍了 DbModelBuilder.Types<T> 方法:

Begins configuration of a lightweight convention that applies to all entities and complex types in the model that inherit from or implement the type specified by the generic argument. This method does not register types as part of the model.

这意味着您可以像这样配置基础实体 class:

modelBuilder.Types<Entity>().Configure(c =>
{
    c.HasKey(e => e.Id);
});

这样您就不必为从 Entity.

继承的每个类型执行此操作