C# 如何使用MVC3在EntityFramework(EF)4.3中根据某些属性值切换到不同的表

C# 如何使用MVC3在EntityFramework(EF)4.3中根据某些属性值切换到不同的表,c#,asp.net,asp.net-mvc-3,entity-framework,code-first,C#,Asp.net,Asp.net Mvc 3,Entity Framework,Code First,假设我有一个基类组件和两个派生类ComponentA和ComponentB,如下所示: public class Component { public int ComponentID {get; set;} public int ComponentType {get; set;} // some other statements ... } 然后 public class ComponentA : Component { // some statements ... }

假设我有一个基类组件和两个派生类ComponentA和ComponentB,如下所示:

public class Component
{
   public int ComponentID {get; set;}
   public int ComponentType {get; set;}
   // some other statements ...
}
然后

public class ComponentA : Component
{
   // some statements ...
}

public class ComponentB : Component
{
   // some statements ...
}
现在,根据Component类中COmponentType的值,如何切换到ComponentA或ComponentB并检索它们的相关数据

这是如何在edmx中实现这一点的示例之一,但我想知道是否有任何方法可以在EF中的代码优先方法中实现同样的事情。

您不需要componentType属性。当使用EF上的继承时,可以使用OfType方法从派生类获取数据。有点像

myContent.Component.OfType<ComponentA>();
myContent.Component.OfType();