我有两个表Articles
和Events
,我想为这两种类型的用户提供评论功能。难的部分是我想使用导航属性返回属于给定EF对象的注释。EF代码第一和虚拟属性
public class Article
{
public virtual ICollection<Comment> Comments { get; set; }
/* more properties here */
}
public class Event
{
public virtual ICollection<Comment> Comments { get; set; }
/* more properties here */
}
public class Comment
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CommentId { get; set; }
public string Msg { get; set; }
public DateTime SentAt { get; set; }
public int TargetId { get; set; }
public CommentTargeType TargetType { get; set; }
}
public enum CommentTargeType
{
Article,
Event
}
正如你所看到的TargetId
会的Article
或Event
和TargetType
的ID来区分这两种类型。 那么,有没有办法做到这一点?或者创建一个ArticleComments和一个EventComments类型会更好吗?
你有什么工作,你只需要小心你的阅读,并写道TargetType是正确的。 – 2013-02-22 18:02:39