Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据MVC3和EF中的某些属性值在两个表中选择一个表_C#_Asp.net_Entity Framework_Asp.net Mvc 3 - Fatal编程技术网

C# 根据MVC3和EF中的某些属性值在两个表中选择一个表

C# 根据MVC3和EF中的某些属性值在两个表中选择一个表,c#,asp.net,entity-framework,asp.net-mvc-3,C#,Asp.net,Entity Framework,Asp.net Mvc 3,假设我有一个基类组件和两个派生类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中的代码优先方法中实现同样的事情

您可以先用代码找到每个层次结构的TPH表

您可以删除ComponentType属性,EF将自动创建一个带有鉴别器列的组件表,该列用于区分子类之间的duh

如果您需要鉴别器列的特定列名和/或数据类型,则可以直接或通过EntityTypeConfiguration(例如,通过

Map(m => m.Requires("ComponentType").HasValue(1);
对于每个子类型。显然,HasValue每次都使用不同的值

您将在上下文中创建属性DbSet。这将返回所有组件,无论其类型如何。如果您只需要组件

哦,组件应该是一个抽象类

context.Components.OfType<ComponentA>(). ...