将具有不同验证要求的两个实体映射到单个表
是否可以将两个实体映射到单个表(每个层次结构的表),但对它们施加不同的验证要求?但在大多数情况下,它们是相同的。
public class Email
{
[Key]
public int Id { get; set; }
[StringLength(4000)]
public String Message { get; set; }
}
public class Tweet
{
[Key]
public int Id { get; set; }
[StringLength(140)]
public String Message { get; set; }
}
然后将这两个实体映射到一个表:
Table: Messages
Id int IDENTITY
Discrimator string
Message nvarchar(4000)
Is it possible to map two entities to a single table (table per hierarchy) but impose different validation requirements on them? For the most part they are the same thing though.
public class Email
{
[Key]
public int Id { get; set; }
[StringLength(4000)]
public String Message { get; set; }
}
public class Tweet
{
[Key]
public int Id { get; set; }
[StringLength(140)]
public String Message { get; set; }
}
And then have both these entities map to a single table:
Table: Messages
Id int IDENTITY
Discrimator string
Message nvarchar(4000)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这是不可能的,因为您的两个类根本无法映射到一张表。它需要定义
Id
和Message
的基类以及从该基类派生的两个空类。共享属性必须在基类中定义 = 它们仅定义一次并且只有一个属性。No it is not possible because your two classes cannot be mapped to one table at all. It would require base class defining both
Id
andMessage
and two empty classes derived from that base class. Shared properties must be defined in a base class = they are defined only once and have only one attribute.