Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
Entity framework 实体框架代码优先继承问题_Entity Framework_Inheritance_Entity Framework 4.1_Ef Code First - Fatal编程技术网

Entity framework 实体框架代码优先继承问题

Entity framework 实体框架代码优先继承问题,entity-framework,inheritance,entity-framework-4.1,ef-code-first,Entity Framework,Inheritance,Entity Framework 4.1,Ef Code First,我有流式层次结构的代码优先实现 BaseContact{ Public int Id{get;set;} public string Name{get;set;} //.. } Person:BaseContact{ public string Designation{get;set;} //.. } Company:BaseContact{ public int NumOfEmployees{get;set;} //.. } 我只想使用Id值来

我有流式层次结构的代码优先实现

 BaseContact{
   Public int Id{get;set;}
   public string Name{get;set;} 
//..
 }

 Person:BaseContact{

   public string Designation{get;set;} 
//..
 }
Company:BaseContact{
     public int NumOfEmployees{get;set;} 
//..
 }

我只想使用
Id
值来识别个人或公司?目前,我正在使用反射来确定它是个人还是公司。有没有其他方法可以在不做太多工作的情况下识别它?

如果不了解如何初始化类,我假设您有一个表,每个具体类型的方法

您不能仅从ID执行此操作,因为您不知道该ID属于哪个表。“人员”表中的ID 2与“公司”中的ID 3是不同的实体。唯一实用的仅从ID识别的方法是使用逐层次表的方法并检查类型描述符

一些好的参考资料


也可以使用简单的is语句代替反射。Ie
如果(实体是公司)

没有看到您如何初始化您的类,我假设您有一个表,每个具体类型的方法

您不能仅从ID执行此操作,因为您不知道该ID属于哪个表。“人员”表中的ID 2与“公司”中的ID 3是不同的实体。唯一实用的仅从ID识别的方法是使用逐层次表的方法并检查类型描述符

一些好的参考资料

也可以使用简单的is语句代替反射。Ie
如果(实体是公司)

在您的BaseContact(假设它是一个抽象类)中添加抽象属性,该属性将由其他两个类实现。使用枚举标识属性类型,如下所示

 public enum MyType
    {
     Person, 
     Company,
    };

    public abstract class BaseContact{
       public abstract MyType ContactType{get;}   
     }

    public class Person:BaseContact
   {

    public override MyType ContactType
    {
      get
      {
        return MyType.Person;
      }      
    }
   }

public class Company:BaseContact
{
    public override MyType ContactType
    {
      get
       {
         return MyType.Company;
       }  
    } 
 }
使用BaseContact存储库检索实体,并使用enum进行类型分离。

在BaseContact中(假设它是一个抽象类),添加将由其他两个类实现的抽象属性。使用enum按如下方式标识属性类型

 public enum MyType
    {
     Person, 
     Company,
    };

    public abstract class BaseContact{
       public abstract MyType ContactType{get;}   
     }

    public class Person:BaseContact
   {

    public override MyType ContactType
    {
      get
      {
        return MyType.Person;
      }      
    }
   }

public class Company:BaseContact
{
    public override MyType ContactType
    {
      get
       {
         return MyType.Company;
       }  
    } 
 }

使用BaseContact存储库检索实体并使用enum进行类型分离。

仅使用
Id
值,您想用什么标识个人或公司?因为在MVC层中,根据对象的类型,我想实施几种表示策略。您想用什么标识个人或公司,仅使用
Id
值?因为在MVC层中,根据对象的类型,我希望实现几种表示策略。