C# 获取实体框架中多对多关系的ICollection数据
我一直在努力在实体框架中获得ICollection值。我正在使用版本6 类导航函数 所以当我运行下面的代码时,我得到了所有导航控制器的导航功能C# 获取实体框架中多对多关系的ICollection数据,c#,.net,asp.net-mvc,entity-framework,ado.net-entity-data-model,C#,.net,Asp.net Mvc,Entity Framework,Ado.net Entity Data Model,我一直在努力在实体框架中获得ICollection值。我正在使用版本6 类导航函数 所以当我运行下面的代码时,我得到了所有导航控制器的导航功能 public IEnumerable<Navigation_Functions> GetAllFunctions() { using(var _uow = new FunctionsNavigation_UnitOfWork()) { var entities = _u
public IEnumerable<Navigation_Functions> GetAllFunctions()
{
using(var _uow = new FunctionsNavigation_UnitOfWork())
{
var entities = _uow.Navigation_Functions_Repository.GetAll();
return entities.ToList();
}
}
public IEnumerable GetAllFunctions()
{
使用(var\u uow=新函数navigation\u UnitOfWork())
{
var entities=_uow.Navigation_Functions_Repository.GetAll();
返回实体。ToList();
}
}
我将虚拟添加到模型中
public virtual ICollection<Navigation_Functions> Navigation_Functions { get; set; }
public virtual ICollection<Navigation_FunctionController> Navigation_FunctionController { get; set; }
公共虚拟ICollection导航函数{get;set;}
公共虚拟ICollection导航_FunctionController{get;set;}
我在读取数据时遇到以下错误
您不需要创建多对多关系表,Entity framework将为您创建多对多关系表。您只需将属性声明为虚拟,所有属性都使用include方法。
更多详细信息或确保将ICollection方法设置为“虚拟” 例如 公共虚拟ICollection课程{get;set;} 以下是一个例子:
请记住,如果交集表包含一个或多个属性(它们不是主键的一部分),则不要创建交集表,只需在两个实体中使用ICollection即可。但是,如果交叉点中有一个或多个特性,则创建交叉点类。这就是EF Designer的工作原理。您的问题是什么?我无法在Icollection中获得价值,即Icollection Navigation\u Function Controlleri我的问题中有上载图像,请在上面将您的收藏标记为
virtual
或使用Include()
急切地加载它。我需要使用fluent API吗?不需要。删除导航\u函数InControl类,然后在公共虚拟ICollection导航\u函数控制器{get;set;}之前使用虚拟。我仍然没有结果,我删除了中间类并添加了虚拟,参考我在上面问题中更新的屏幕截图,您是否更新了数据库?确保您创建了多对多关系表,确保您拥有给定id的数据
[Table("Navigation_FunctionInController")]
public class Navigation_FunctionInController
{
public Navigation_FunctionInController()
{
}
[Key]
public int FunctionInController_ID { get; set; }
[Key]
[ForeignKey("Navigation_Functions")]
public int Function_ID { get; set; }
[Key]
[ForeignKey("Navigation_FunctionController")]
public int ControllerID { get; set; }
public Navigation_FunctionController Navigation_FunctionController { get; set; }
public Navigation_Functions Navigation_Functions { get; set; }
}
public IEnumerable<Navigation_Functions> GetAllFunctions()
{
using(var _uow = new FunctionsNavigation_UnitOfWork())
{
var entities = _uow.Navigation_Functions_Repository.GetAll();
return entities.ToList();
}
}
public virtual ICollection<Navigation_Functions> Navigation_Functions { get; set; }
public virtual ICollection<Navigation_FunctionController> Navigation_FunctionController { get; set; }