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?