Code
属性的InheritanceMappingAttribute
属性的目的是什么?
文档说它与IsDiscriminator
属性有关。但是,我也不知道IsDiscriminator
属性。
我正在读这个例子:
public enum ShapeType
{
Square = 0, Circle = 1
}
[Table(Name = "Shape")]
[InheritanceMapping(Code = ShapeType.Square, Type = typeof(Square),
IsDefault = true)]
[InheritanceMapping(Code = ShapeType.Circle, Type = typeof(Circle))]
abstract public class Shape
{
[Column(IsDiscriminator = true)]
public ShapeType ShapeType = 0;
}
public class Square : Shape
{
[Column]
public int Side = 0;
}
public class Circle : Shape
{
[Column]
public int Radius = 0;
}
就我而言,我有三个表,Persons
,Clients
,Functionaries
。三个班级Person
(摘要),Client
和Functionary
。也就是说,在Person
表中是Client
和Functionary
之间的公共数据。在Clients
表中是Client
的数据。在Functionaries
表中是Functionary
的数据。
在这种情况下如何进行映射?
答案 0 :(得分:0)
使用EF6,您不必配置映射。只需创建类,EF完成其余的工作。
public abstract class Person
{
public int Id { get; set; }
}
[Table("Clients")]
public class Client : Person
{
}
[Table("Functionaries")]
public class Functionary : Person
{
}
EF创建3个表并设置关系。
添加类型
context.Persons.Add(new Client());
context.Persons.Add(new Functionary());
您可以使用
查询类型Persons.OfType<Client>()