我已经使用 THP 实现了具有简单继承的代码优先数据库架构:
我需要查询所有类型的所有通知。
TargetUser
表中的属性NotificationUser
是一个关联。我正在尝试执行下一个代码:
var notifications = _context.Notifications;
foreach (var notification in notifications)
{
Debug.WriteLine((notification is NotificationUser)? ((NotificationUser) notification).TargetUser?.Name : "-");
}
在数据库TargetUser
中属性设置为正确的外键,但在代码中我没有得到任何结果。延迟加载已启用。
是否可以用户急切加载?我已经尝试过写_context.Notifications.Include('TargetUser')
它会引发异常。
更新。例外是:
A specified Include path is not valid. The EntityType 'Core.Concrete.NotificationBase' does not declare a navigation property with the name 'TargetUser'.
试图将此答案修改为:
var notifications = _context.Notifications.OfType<NotificationUser>()
.Include(n => n.TargetUser)
.Cast<NotificationBase>()
.Union(_context.Notifications.OfType<NotificationPlace>()
但仍然抛出相同的异常。