简体   繁体   English

使用基于抽象类的子EF实体属性

[英]Working with child EF entity properties based on an abstract class

I am using Entity Framework 6 Code First, with c#4.0 and Visual Studio 2012. The following are code snippets. 我正在使用实体框架6代码优先,使用c#4.0和Visual Studio 2012.以下是代码片段。

I have a base abstract class: 我有一个基础抽象类:

public abstract class Person
{
    public int PersonID { get; set; }
    public String Name { get; set; }        
}

from which I derive three entities: 从中派生出三个实体:

public class Contact : Person
{
    public Nullable<int> NHSTrustID { get; set; }
    public virtual NHSTrust NHSTrust { get; set; }        
}

 public class User : Person
{
    public int NHSTrustID { get; set; }
    public virtual NHSTrust NHSTrust { get; set; }
}

public class Notifier : Person
{
    public int NotifierTypeID { get; set; }
    public virtual NotifierType NotifierType { get; set; }        
}

The entities are declared as: 实体声明为:

public DbSet<Person> Persons { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Contact> Contacts { get; set; }
public DbSet<Notifier> Notifiers { get; set; }

so that the Persons entity can be queries as well. 这样人员实体也可以是查询。

A person who could belong to any of the three derived entites. 一个person可以属于三个派生者中的任何一个。 I need to know if a person has the property NHSTrust . 我需要知道一个person是否拥有NHSTrust的财产。 ( Users must have an NHSTrust , for Contacts it is optional and Notifiers don't have an NHS Trust. Users必须拥有NHSTrust ,对于Contacts它是可选的,并且Notifiers没有NHS信任。

I can see how to do it with lots of code, but is there an elegant way of doing this? 我可以看到如何使用大量代码来完成它,但是有一种优雅的方式吗?

UPDATE My current 'solution' is: 更新我目前的“解决方案”是:

var tempPerson = dbContext.Persons.Find(personID);  
NHSTrust nHSTrust = null;

if (tempPerson is Contact)
{
    nHSTrust = dbContext.Contacts.Find(personID).NHSTrust;
}
else if (tempPerson is User)
{
    nHSTrust = dbContext.Users.Find(personID).NHSTrust;
}
if (nHSTrust != null) { // do something}

Is there a way of doing this with just a single trip to that database. 有没有办法只需一次访问该数据库即可完成此操作。

Create another level of your inheritance hierarchy: 创建继承层次结构的另一个级别:

public abstract class TrustablePerson : Person
{
    public NHSTrust NHSTrust { get; set; }
}
public class Contact : TrustablePerson
{
    public Nullable<int> NHSTrustID { get; set; }
}
public class User : TrustablePerson
{
    public int NHSTrustID { get; set; }
}

Now you can add a TrustablePerson to your context that gets items of that type. 现在,您可以将TrustablePerson添加到您的上下文中,以获取该类型的项目。

This seems like an abstraction-level issue, where an abstract class is probably unneeded. 这似乎是一个抽象级问题,可能不需要抽象类。 It doesn't really save you anything in such small classes. 在这么小的课程中,它并没有真正为你节省任何东西。 Additionally, because some of the derived types are so similar, if you really want to abstract this, you should probably use an interface. 另外,因为某些派生类型非常相似,如果你真的想要抽象它,你应该使用一个接口。

However, to answer your question, you can use the is operator, or even use linq. 但是,要回答您的问题,您可以使用is运算符,甚至可以使用linq。 It will be fairly long and a bit messy. 这将是相当长的,有点凌乱。 Really, though, your problem is the abstraction. 但实际上,你的问题就是抽象。

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

相关问题 EF CORE 5.0.1 配置抽象 class 作为 model 实体被忽略,但在派生模型中访问其属性 - EF CORE 5.0.1 Configuring abstract class to be ignored as an model entity but access its properties in derived models 基于抽象实体(EF 4.1)的包含属性的问题 - Problem with include property based on Abstract Entity (EF 4.1) 从抽象类对象列表访问子类的属性 - Accessing properties of a child class from list of abstract class objects 如何使用抽象类型的导航属性实现基于 model 的 EF Core 抽象 - How to implement EF Core abstract based model with navigation properties of abstract types 通过LINQ基于抽象父属性加载具体子类型 - Loading concrete child types based on abstract parent properties via LINQ EF Core 如何根据对象属性更新实体 - EF Core how to update entity based on object properties 映射抽象类,应该将公共属性映射到抽象类或子类中 - Mapping abstract class, should common properties be mapped inside abstract or child class EF7 - 实体 class 的属性 object 未被填充 - EF7 - Properties object from entity class are not being filled EF Core基础实体类的继承和只读属性 - EF Core base entity class inheritance and readonly properties EF实体的子类未在DBContext上提交,因为它不是模型的一部分 - Child class of EF Entity not being committed on the DBContext because it is not part of the model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM