【问题标题】:How to switch to different tables on the basis of some attribute value in Entity Framework (EF) 4.3 using MVC3如何使用MVC3在Entity Framework(EF)4.3中根据某些属性值切换到不同的表
【发布时间】:2012-03-29 09:20:57
【问题描述】:

假设我有一个基类 Component 和两个派生类 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 中的 Code First 方法中执行相同操作。 http://www.c-sharpcorner.com/UploadFile/ff2f08/entity-framework-4-0-tph-part-2/

【问题讨论】:

    标签: c# asp.net asp.net-mvc-3 entity-framework code-first


    【解决方案1】:

    您不需要 componentType 属性。在 EF 上使用继承时,可以使用 OfType 方法从派生类中获取数据。类似的东西;

    myContent.Component.OfType<ComponentA>();
    

    【讨论】:

      猜你喜欢
      • 2012-04-17
      • 2012-12-20
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-05
      相关资源
      最近更新 更多