关系“{0}”未加载,因为类型“{1}”不可用

The relationship '{0}' was not loaded because the type '{1}' is not available

我得到:The relationship 'Echipieri.Data.Event_Hosts' was not loaded because the type 'Echipieri.Data.User' is not available.

我找到了一些关于这个问题的 post 但没有找到适合我的解决方案。

这是我的实体:

    public class User
    {
        public int ID { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string AboutSelf { get; set; }
        public DateTime BirthDate { get; set; }

        //relational properties        
        public Location Location { get; set; }
        public int LocationID { get; set; }

        public virtual ICollection<Reputation> Reputation { get; set; }
        public virtual ICollection<Category> Hobbies { get; set; }
        public virtual ICollection<Group> Groups { get; set; }
        public virtual ICollection<Event> Events { get; set; }
        public virtual ICollection<Post> Posts { get; set; }

        public User()
        {
            Reputation = new HashSet<Reputation>();
            Hobbies = new HashSet<Category>();
            Groups = new HashSet<Group>();
            Events = new HashSet<Event>();
            Posts = new HashSet<Post>();
        }
    }

public class Event
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }

    //relational properties
    public Location Location { get; set; }
    public int LocationID { get; set; }
    public Category Category { get; set; }
    public int CategoryID { get; set; }

    public virtual ICollection<User> Hosts { get; set; }
    public virtual ICollection<User> Going { get; set; }
    public virtual ICollection<Post> Posts { get; set; }

    public Event()
    {
        Hosts = new HashSet<User>();
        Going = new HashSet<User>();
        Posts = new HashSet<Post>();
    }
}

和映射(流畅API):

public UserMap()
{
    Property(x => x.Email).IsRequired();
    Property(x => x.Password).IsRequired();
    Property(x => x.FirstName).IsRequired();
    Property(x => x.LastName).IsRequired();
    Property(x => x.AboutSelf).IsOptional();
    Property(x => x.BirthDate).IsRequired();

    //relational properties
    HasRequired(x => x.Location).WithMany(x => x.Users).HasForeignKey(x => x.LocationID);

    HasMany(x => x.Reputation).WithRequired(x => x.User).HasForeignKey(x => x.UserID);
    HasMany(x => x.Hobbies).WithMany(x => x.Subscribers)
                            .Map(x => 
                            {
                                x.ToTable("User_Category");
                                x.MapLeftKey("User");
                                x.MapRightKey("Category");
                            });
    HasMany(x => x.Groups).WithMany(x => x.Members)
                            .Map(x =>
                            {
                                x.ToTable("User_Group");
                                x.MapLeftKey("User");
                                x.MapRightKey("Group");
                            });
    HasMany(x => x.Events).WithMany(x => x.Going)
                            .Map(x => 
                            {
                                x.ToTable("User_Events");
                                x.MapLeftKey("User");
                                x.MapRightKey("Event");
                            });
    HasMany(x => x.Posts).WithRequired(x => x.Author).HasForeignKey(x => x.AuthorID);
}

public EventMap()
{
    Property(x => x.Title).IsRequired();
    Property(x => x.Description).IsRequired();
    Property(x => x.StartDate).IsRequired();
    Property(x => x.EndDate).IsRequired();

    //relational properties
    HasRequired(x => x.Location).WithMany(x => x.Events).HasForeignKey(x => x.LocationID);
    HasRequired(x => x.Category).WithMany(x => x.Events).HasForeignKey(x => x.CategoryID);

    HasMany(x => x.Hosts).WithMany(x => x.Events)
                            .Map(x =>
                            {
                                x.ToTable("Event_Hosts");
                                x.MapLeftKey("Event");
                                x.MapRightKey("Host");
                            });
    HasMany(x => x.Going).WithMany(x => x.Events)
                            .Map(x =>
                            {
                                x.ToTable("User_Events");
                                x.MapLeftKey("User");
                                x.MapRightKey("Event");
                            });
    HasMany(x => x.Posts);
}

根据我的研究(但找不到具体答案),问题是 HostsGoing 属于同一类型(User)有人可以告诉我如果这是问题所在?

我不完全确定这是否是您遇到的错误的根源,但是如果您查看 EventMap(我假设它是某些 EntityTypeConfiguration 的构造函数):

HasMany(x => x.Hosts).WithMany(x => x.Events)
                     .Map(x =>
                     {
                         x.ToTable("Event_Hosts");
                         x.MapLeftKey("Event");
                         x.MapRightKey("Host");
                     });
HasMany(x => x.Going).WithMany(x => x.Events)
                     .Map(x =>
                     {
                         x.ToTable("User_Events");
                         x.MapLeftKey("User");
                         x.MapRightKey("Event");
                     });

在本节中,您要将两个不同的关系映射到相同的 User.Events 属性。这是一个问题,例如,当您将项目添加到 User.Events 时——添加的 Event 是否会在 HostsGoing 中有一个新用户?

您需要为每个关系建立一个单独的 属性。例如,将此添加到 User:

public virtual ICollection<Event> HostedEvents { get; set; }

和你的主人关系可以

HasMany(x => x.Hosts).WithMany(x => x.HostedEvents)

来源:Schema specified is not valid. Errors: The relationship was not loaded because the type is not available


如果您希望 User.Events 自动包含托管事件和用户将要参加的事件,则导航映射不能使用 属性,并且 属性需要 return 某种查询。

就我而言,问题是因为 table 名称冲突。

我有另一个 edmx 模型,其中有一些 table 具有相似的名称。只需单击设计器中的 table,然后在属性中将名称更改为不同的名称!