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

Posted

技术标签:

【中文标题】未加载关系“0”,因为类型“1”不可用【英文标题】:The relationship '0' was not loaded because the type '1' is not available未加载关系“0”,因为类型“1”不可用 【发布时间】:2015-05-10 20:18:27 【问题描述】:

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

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

这是我的实体:

    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>();
    

以及映射(Fluent 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)有人可以告诉我这是不是问题所在?

【问题讨论】:

【参考方案1】:

我不完全确定这是否是您遇到的错误的根源,但如果您查看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 自动包含托管事件和用户将要访问的事件,则导航映射不能使用该属性,并且该属性需要返回某种查询。

【讨论】:

【参考方案2】:

在我的情况下,问题是因为表名冲突。

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

【讨论】:

我只是想添加这是我的问题的解决方案 - 我有两个模型引用同一个表。非常困难和神秘的事情要弄清楚。最佳做法应该是将模型名称添加到我认为的表中(例如 mePerson for Person 表)

以上是关于未加载关系“0”,因为类型“1”不可用的主要内容,如果未能解决你的问题,请参考以下文章

颤动旋转的文本未填充可用空间

Xcode 设备不可用,未找到运行时配置文件

Chrome 刷新右键重新加载选项不可用

服务器未连接。部署不可用。 IntelliJ 13 和 Tomcat 7

QMYSQL 可用但无法加载

组件中不可用的道具[重复]