简体   繁体   English

指定的EF6模式无效。 错误:由于类型y不可用,所以未加载关系x

[英]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.

相关问题 指定的架构无效。 错误:未加载关系,因为类型不可用 - Schema specified is not valid. Errors: The relationship was not loaded because the type is not available 指定的 EntityFramework 架构无效。 错误: - EntityFramework Schema specified is not valid. Errors: EF6:指定的架构无效; 模糊映射 - EF6: Schema specified is not valid; ambiguous mapping 指定的架构无效。 错误:同名的多种类型 - Schema specified is not valid. Errors: Multiple types with the name EF6模型优先:指定的架构无效,错误0040 - EF6 Model First: schema specified is not valid, error 0040 未加载FK关系,因为该类型不可用 - The FK relationship was not loaded because the type is not available 由于类型xx不可用,因此未加载关系xx - The relationship xx was not loaded because the type xx is not available EF映射同一表的多个属性。 未加载关系“”,因为类型“”不可用 - EF Mapping multiple properties same table. The relationship '' was not loaded because the type '' is not available 未加载关系,因为在托管网站时类型不可用错误 - The relationship was not loaded because the type is not available error while hosting website 带有oracle的EF-“在配置中找不到指定的存储提供程序,或者它无效。” - EF with oracle - “The specified store provider cannot be found in the configuration, or is not valid.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM