【问题标题】:Get ICollection data for many-to-many relationship in Entity framework获取实体框架中多对多关系的 ICollection 数据
【发布时间】:2015-07-11 08:51:32
【问题描述】:

我很难在实体框架中获得 ICollection 值。我使用的是第 6 版。

类 Navigation_Functions

[Table("Navigation_Functions")]
public class Navigation_Functions
{
    public Navigation_Functions()
    {}

    [Key]
    public int Function_ID { get; set; }

    [StringLength(250)]
    [Required(ErrorMessage = "Required Title")]
    [Display(Name = "Function Title")]
    public string FunctionName { get; set; }

    [Required(ErrorMessage = "Required Hierarchy Level")]
    [Display(Name = "Hierarchy Level")]
    public int Hierarchy_Level { get; set; }

    public ICollection<Navigation_FunctionController> Navigation_FunctionController { get; set; }
}

类 Navigation_Controller

[Table("Navigation_FunctionController")]
public class Navigation_FunctionController
{
    public Navigation_FunctionController()
    { }

    [Key]
    public int ControllerID { get; set; }

    [StringLength(250)]
    [Required]
    public string ControllerName { get; set; }

    public ICollection<Navigation_Functions> Navigation_Functions { get; set; }

}

打破多对多关系的中产阶级

[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; }

}

所以当我运行以下代码时,我会得到所有用于 navigation_function 的 navigation_controller

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

我收到以下错误以读取数据

【问题讨论】:

  • 你有什么问题?
  • 我无法在 Icollection 中获取值,即 ICollection Navigation_FunctionController
  • 我的问题中有上传图片,参考上面
  • 将您的收藏标记为virtual 或使用Include() 急切地加载它。

标签: c# .net asp.net-mvc entity-framework ado.net-entity-data-model


【解决方案1】:

确保将 ICollection 方法设置为“虚拟” 例如 公共虚拟ICollection课程{得到;放; }

这是一个例子: http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx

请记住,如果交集表包含一个或多个属性(不是主键的一部分),则不要创建交集表,只需在两个实体中使用 ICollection。但是,如果交集中有一个或多个属性,则创建交集类。这就是 EF 设计器的工作原理。

【讨论】:

    【解决方案2】:

    您不需要创建多对多关系表,实体框架会为您完成。您所需要的只是将您的属性声明为虚拟的,所有这些都使用 include 方法。 更多详情herehere

    【讨论】:

    • 我需要为此使用 fluent API 吗??
    • 不,不需要。删除 Navigation_FunctionInController 类,然后使用 public virtual ICollection Navigation_FunctionController { get;放; }
    • 我仍然没有结果,我删除了中产阶级并添加了虚拟,请参阅我在上面的问题中更新的屏幕截图
    • 你更新你的数据库了吗?确保您创建了多对多关系表,确保您拥有给定 id 的数据
    猜你喜欢
    • 1970-01-01
    • 2018-10-29
    • 2017-01-28
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多