开发者

Entity Framework Inheritance - determine object type

开发者 https://www.devze.com 2023-02-05 13:47 出处:网络
I have an Entity Framework Model similar to this: Person Employee (Inherits Person) Contact (Inherits Person)

I have an Entity Framework Model similar to this:

  1. Person
  2. Employee (Inherits Person)
  3. Contact (Inherits Person)

I can Add,Query (Using OfType), and Update Employees and Contacts with no problem. However, I can not determine what type a Person object is. Say for example:

var person = entities.People.Single(p => p.Id == 5); 

How can I do this:

if (开发者_开发问答person.IsEmployee){
//do something
} else if (person.IsContact) {
// do something else
}

Alternatively, I can settle for this:

if (person.IsOfType<Employee>()){
// do something
} else if (person.IsOfType<Contact>()) {
// do something else
}

Is there a way?


if (person is Employee){
//do something
} else if (person is Contact) {
// do something else
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号