[英]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.