实体框架 - 将SubClass的数据存储在与MainClass数据相同的数据表中

时间:2016-04-20 07:38:04

标签: entity-framework

使用.Net Entity Framework我想将“SubClass”的数据存储在与“MainClass”的数据相同的数据表中。 SubClass没有Id,它只是为了重新使用我想要从MainClass创建一个Class的一部分。

public class MainClass
    {
     public int Id { get; set; }
     public SubClass Property1 { get; set; }
    }

public class SubClass
    {
    //This class has no Id and its data should be stored in the same table as the data of MainClass
    public string Property2 { get; set; }
    }

2 个答案:

答案 0 :(得分:0)

这应该会带你朝着正确的方向前进。您必须从MainClass继承并确保两个类都映射到数据库中的同一个表。我不确定为什么你希望SubClass成为mainclass的属性,如果这是故意的,或者你不确定继承是如何工作的......

[Table("MainClassTableName")]
public class MainClass
    {
       public int Id { get; set; }
    }

[Table("MainClassTableName")]
public class SubClass : MainClass
    {
    //This class has no Id and its data should be stored in the same table as the data of MainClass
       public string Property2 { get; set; }
    }

答案 1 :(得分:0)

好的,那你正在寻找复杂的类型。 - Gert Arnold 4月20日19:58