Entity framework 关系';{0}';未加载,因为类型为';{1} &x27;不可用
我得到:Entity framework 关系';{0}';未加载,因为类型为';{1} &x27;不可用,entity-framework,ef-fluent-api,Entity Framework,Ef Fluent Api,我得到:没有加载关系“Echipieri.Data.Event\u Hosts”,因为类型“Echipieri.Data.User”不可用。 我已经找到了一些关于这个问题的帖子,但没有找到解决方案 以下是我的实体: public class User { public int ID { get; set; } public string Email { get; set; } public string Password { get
没有加载关系“Echipieri.Data.Event\u Hosts”,因为类型“Echipieri.Data.User”不可用。
我已经找到了一些关于这个问题的帖子,但没有找到解决方案
以下是我的实体:
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>();
}
}
根据我的研究(但找不到具体答案),问题是
主机
和正在运行的
属于同一类型(用户
),有人能告诉我这是否是问题吗?我不完全确定这是否是您遇到的错误的根源,但如果您查看事件图
(我假设它是某些EntityTypeConfiguration
的构造函数):
在本节中,您将两个不同的关系映射到同一个User.Events
属性。这是一个问题,例如,当您向User.Events
添加一个项目时——添加的Event
是否会在主机中有一个新用户
或正在运行
每个关系都需要有一个单独的属性。例如,将此属性添加到User
:
public virtual ICollection<Event> HostedEvents { get; set; }
资料来源:
如果希望
User.Events
自动包含托管事件和用户将要访问的事件,则导航映射无法使用该属性,该属性需要返回某种类型的查询。在我的示例中,问题是因为表名冲突
我有另一个edmx模型,它有一些具有类似名称的表。只需在设计器中单击该表,然后在属性中将名称更改为不同的名称!
HostedEvents
实际上就是我最终要做的事情,并且重新认识到,将事件
用作正在进行的事件和托管的属性是没有意义的锿。
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");
});
public virtual ICollection<Event> HostedEvents { get; set; }
HasMany(x => x.Hosts).WithMany(x => x.HostedEvents)