[英]EF6 Schema specified is not valid. Errors: The relationship x was not loaded because the type y is not available
This is asked many times, I know where the exact issue is but I am trying to avoid it. 这个问题被问了很多遍,我知道确切的问题在哪里,但是我在努力避免。
Simplified POCO: 简化的POCO:
public class TaskEntity
{
public int TaskId { get; set; }
public int? AssignedToId { get; set; }
public virtual UserEntity AssignedTo { get; set; }
public int CreatedById { get; set; }
public virtual UserEntity CreatedBy { get; set; }
public int? ClosedById { get; set; }
public virtual UserEntity ClosedBy { get; set; }
}
public class UserEntity
{
public List<TaskEntity> TaskId { get; set; }
public int UserId { get; set; }
public string Name { get; set; }
}
Mappings: 映射:
public class TaskMap : EntityTypeConfiguration<TaskEntity>
{
public TaskMap()
{
ToTable("tTasks");
HasKey(x => x.TaskId);
HasRequired(x => x.CreatedBy).WithMany(x => x.TaskId).HasForeignKey(x => x.CreatedById).WillCascadeOnDelete(false);
HasOptional(x => x.ClosedBy).WithMany(x => x.TaskId).HasForeignKey(x => x.ClosedById).WillCascadeOnDelete(false);
HasOptional(x => x.AssignedTo).WithMany(x => x.TaskId).HasForeignKey(x => x.AssignedToId).WillCascadeOnDelete(false);
}
}
I have read that I should separate UserEntity in 3 different classes and make them inherit from TaskEntity, but this doesn't sounds right as It will be exactly the same user object for all these cases. 我读过我应该将UserEntity分成3个不同的类,并使它们从TaskEntity继承,但这听起来不对,因为在所有这些情况下,它都是完全相同的用户对象。
I am expecting to have table structure as follows: 我期望表结构如下:
tTasks 任务
TaskId | TaskId | [FK]AssignedToId |
[FK] AssignedToId | [FK]CreatedById |
[FK] CreatedById | [FK]ClosedById
[FK] ClosedById
tUsers 用户
UserId | 用户名| Name
名称
Could someone point what am I doing wrong here. 有人可以在这里指出我在做什么错。 Do I need to adjust my mapping somehow in order to get my table created as I expect
我是否需要以某种方式调整映射以便按预期创建表
The answer is yes. 答案是肯定的。 You should adjust your mapping.
您应该调整映射。 What you're doing wrong is in this line:
您在这行中做错了:
public List<TaskEntity> TaskId { get; set; }
public List<TaskEntity> TaskId { get; set; }
. public List<TaskEntity> TaskId { get; set; }
。
In EF you cannot get in the same navigation property all Tasks related to the UserEntity with different foreign keys. 在EF中,您无法在同一导航属性中获得与具有不同外键的UserEntity相关的所有任务。 That means you need a navigation property in UserEntity to be mapped against every navigation property in TaskEntity.
这意味着您需要将UserEntity中的导航属性映射到TaskEntity中的每个导航属性。 And as you have 3 navigation properties in every class you will need to specify which is against which.
并且由于每个类中都有3个导航属性,因此需要指定哪个相对于哪个。 You'll get this:
您会得到:
public class TaskEntity
{
public int TaskId { get; set; }
public int? AssignedToId { get; set; }
[InverseProperty("AssignedTasks")]
public virtual UserEntity AssignedTo { get; set; }
public int CreatedById { get; set; }
[InverseProperty("CreatedTasks")]
public virtual UserEntity CreatedBy { get; set; }
public int? ClosedById { get; set; }
[InverseProperty("ClosedTasks")]
public virtual UserEntity ClosedBy { get; set; }
}
public class UserEntity
{
public int UserId { get; set; }
public string Name { get; set; }
[InverseProperty("AssignedTo")]
public virtual ICollection<TaskEntity> AssignedTasks {get; set; }
[InverseProperty("CreatedBy")]
public virtual ICollection<TaskEntity> CreatedTasks {get; set; }
[InverseProperty("ClosedBy")]
public virtual ICollection<TaskEntity> ClosedTasks {get; set; }
}
With this all the mapping is done with the annotations and you can remove the TaskMap
class. 这样,所有映射都通过注释完成,您可以删除
TaskMap
类。
You can add a List<TaskEntity> Tasks
to your UserEntity that aggregates the results from the 3 previous navigation properties, but the aggregation will be done after the data is loaded and you cannot use it in Linq queries. 您可以将
List<TaskEntity> Tasks
添加到您的UserEntity中,以汇总前3个导航属性的结果,但是汇总将在加载数据之后完成,您将无法在Linq查询中使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.