简体   繁体   English

每种类型的表代码优先-如何区分基本类型是子类型

[英]Table-per-Type Code First - How to differentiate if Base type is sub type

I'm certain that my issue extends from a misinterpretation of exactly how Base type/Sub type entities are related, but bear with me. 我敢肯定,我的问题源于对基本类型/子类型实体之间确切关联的误解,但请允许我。

Say I have two classes: 说我有两节课:

public class Business
{
    public int UniqueID { get; set; }
    public string Name { get; set; }
}

public class BusinessType1 : Business
{
    public string SomeRelatedThing { get; set; }
}

public class BusinessType2 : Business
{
    public string SomeNotRelatedThing { get; set; }
}

In my application, I am using the base type for the most part. 在我的应用程序中,大部分时间都是在使用基本类型。 Some pages are contextual to the Sub types however, but need to be versatile enough to handle either (DNRY). 但是,某些页面与Sub类型有关,但是需要足够多的功能来处理这两种类型(DNRY)。

The page is only given the base type in context, and must load data from either BusinessType1 or BusinessType2. 该页面仅在上下文中被赋予基本类型,并且必须从BusinessType1或BusinessType2加载数据。

The problem I am facing is that I need to ascertain if the base type (Business) is either linked with BusinessType1 or BusinessType2. 我面临的问题是我需要确定基本类型(业务)是否与BusinessType1或BusinessType2链接。 I would prefer to do this without making a query to determine if the key exists in each table. 我更愿意在不进行查询以确定键是否存在于每个表中的情况下执行此操作。

As far as I can see, this is not possible - hence my question; 据我所知,这是不可能的。

Is there a way to achieve this using minimal queries to the DB? 有没有一种方法可以使用对数据库的最少查询来实现这一目标?

I'll try to extend @Hopeless with examples. 我将尝试通过示例扩展@Hopeless。

Fetch all entities and after that determine base type: 获取所有实体,然后确定基本类型:

var entities = context.Business.ToList();
foreach(var baseEntity in entities)
{
    // some common logic for base entity type

    if (baseEntity is BusinessType1)
    {
        var concreteEntity = (BusinessType1)baseEntity;
        // some logic for entity of BusinessType1
    }
}

Fetch entities only with concrete type: 仅获取具有具体类型的实体:

var concreteEntities = context.Business.OfType<BusinessType1>().ToList();
// some logic for entities of BusinessType1

As you can see, you don't need to perform any additional queries with Entity Framework. 如您所见,您不需要使用Entity Framework进行任何其他查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架代码First Table-Per-Type继承包括基本类型 - Entity Framework Code First Table-Per-Type Inheritance include base type 在使用带有实体框架的Table-per-Type时,如何仅获取基表行? - How do I get just the base table rows when using Table-per-Type with Entity Framework? 每个类型的表继承插入问题 - Table-per-type inheritance insert problem 每个类型的表继承插入问题 - Table-per-type inheritance insert problem 实体框架每个类型的表:1:0…*与派生类的FK关系-是基类还是派生类的FK? - Entity Framework table-per-type: 1:0…* FK relationship with a derived class - is FK of base class or derived class? 将EF CodeFirst Base类转换为Inherited类(使用table-per-type) - Converting a EF CodeFirst Base class to a Inherited class (using table-per-type) EF6外键上的第一个按类型列出的表多重性问题 - EF6 Database First Table-per-Type multiplicity issue on foreign key 实体框架6个自定义键,用于每种类型的表继承 - Entity framework 6 custom keys for table-per-type inheritance 实体框架流利的按表类型性能替代 - Entity Framework Fluent, Table-Per-Type performance alternatives EF Table-Per-Type:将新的子代添加到现有父代 - EF Table-Per-Type: Add new child to an existing parent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM