【发布时间】:2019-11-07 15:01:23
【问题描述】:
我有一个关于“交叉”继承的问题。
我们来上课吧。
public class User
{
public string UserName { get; set; }
public ICollection<Notification> Notifications { get; set; }
}
public class Notification
{
public string Message { get; set;}
public int NotificationType { get; set; }
}
这些是基类。
现在我想以此创建 EF Core 模型。
public class EFUser : User
{
public Guid Id { get; set; }
}
public class EFNotification : Notification
{
public Guid Id { get; set; }
public Guid EFUserId { get; set; }
public EFUser EFUser { get; set; }
}
但正如我们所见,EFUser 有一个ICollection<Notification> Notifications,而不是ICollection<EFNotification> Notifications,因此,这个模型可以迁移到数据库。
据我了解,virtual 关键字的使用不会给我一个解决方案,因为public virtual ICollection<Notification> Notifications 不会改变底层类型。
有没有办法实现这样的构造?
本示例的主要思想是实现“类的基本结构”,可以通过包装在 EF Core 对象模型、DTO 等中使用。
【问题讨论】:
-
即使可以做到,它也会在您的数据模型和域模型(我猜是的)之间创建紧密耦合。完全不推荐。将它们分开并使用映射。
标签: c# .net entity-framework inheritance