C# 实体框架中的继承
我想要一个有公共信息的类<代码>公共数据类将在所有表中使用:C# 实体框架中的继承,c#,.net,entity-framework,entity-framework-core,entity-framework-6,C#,.net,Entity Framework,Entity Framework Core,Entity Framework 6,我想要一个有公共信息的类公共数据类将在所有表中使用: public class CommonData { public string CreatedBy { get; set; } = ""; public string ModifiedBy { get; set; } = ""; public Nullable<DateTime> CreateDate { get; set; } = DateTime.Now;
public class CommonData
{
public string CreatedBy { get; set; } = "";
public string ModifiedBy { get; set; } = "";
public Nullable<DateTime> CreateDate { get; set; } = DateTime.Now;
public Nullable<DateTime> ModifiedDate { get; set; } = DateTime.Now;
}
我知道如果我把所有数据放在一个公共类中,我就可以做到这一点
public class CommonClass
{
public string Id { get; set; }
public string CreatedBy { get; set; } = "";
public string ModifiedBy { get; set; } = "";
public Nullable<DateTime> CreateDate { get; set; } = DateTime.Now;
public Nullable<DateTime> ModifiedDate { get; set; } = DateTime.Now;
}
public class Person : CommonClass
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string ZipCode { get; set; }
public string Phone { get; set; }
}
问题是,我看了一下它创建的表格,我觉得他们一定是一个更好的方法。创建表时,它会将所有的CommonData
放在表的第一个字段中,然后再将所有特定的表数据放在第一个字段中。这把事情搞得一团糟
我尝试过这样做,但没有成功。我希望将CommonData
对象中的数据连接到Person
类。这将是理想的情况,但我不知道如何实现这一点
public class Person
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string ZipCode { get; set; }
public string Phone { get; set; }
public CommonData CommonData {get;set;}
}
为什么要关心列的顺序呢?如果您创建了表,然后向CommonClass添加了一个新属性,那么迁移只会将新列添加到表的末尾。对我来说,我也喜欢这种方式。我的观点是,如果希望保持数据库列顺序与对象定义顺序相同,则需要不断手动调整迁移脚本。因为这不是EF Core关心的“特性”。
public class Person
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string ZipCode { get; set; }
public string Phone { get; set; }
public CommonData CommonData {get;set;}
}