1

I'm getting: The relationship 'Echipieri.Data.Event_Hosts' was not loaded because the type 'Echipieri.Data.User' is not available.

I have found a few post about this problem but didn't find a solution for me.

Here are my entities:

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

And the mapping (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);
}

From what I researched (but couldn't find a concrete answer) the problem is that Hosts and Going are of the same type(User) can someone tell me if this is the issue?

Mihai Bratulescu
  • 1,915
  • 3
  • 27
  • 43

2 Answers2

2

I'm not entirely sure if this is the root of the error you're getting, but if you look in EventMap (which I'm assuming is the constructor for some 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");
                     });

In this section, you're mapping two different relationships to the same User.Events property. This is a problem, for example, when you add an item to User.Events -- would that added Event have a new user in Hosts or Going?

You'll need to have a separate property for each relationship. For example, add this to User:

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

and your hosts relationship can be

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

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


If you want User.Events to automatically include both hosted events and events that the user is going to, then the navigational mappings can't use that property, and the property would need to return a query of some sort.

Community
  • 1
  • 1
jjj
  • 4,822
  • 1
  • 16
  • 39
  • `HostedEvents` is actually what I ended up doing and relized it made no sense to use `Events` as a propert for both the going events and the hosted ones. – Mihai Bratulescu May 12 '15 at 14:26
1

In my case, the problem is because of table name conflict.

I have another edmx model that has some tables with similar names. Just click on the table in the designer, then in the properties change the name to something different!

ARZ
  • 2,461
  • 3
  • 34
  • 56
  • I just wanted to add this was the solution for my issue - i had two models referencing the same table. Very difficult and cryptic thing to figure out. Best practice should be to add the model name to the table I think (like mePerson for Person table) – Rob Oct 21 '21 at 19:07