获取Entity框架中多对多关系的ICollection数据

时间:2022-10-04 12:49:25

I have struggling to get ICollection value in entity framework. I am using version 6.

我一直在努力在实体框架中获得ICollection值。我使用的是版本6。

获取Entity框架中多对多关系的ICollection数据

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

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

}

Middle Class to break many-to-many relationship

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

}

so when I run following code, I get all navigation_controller for navigation_function

因此,当我运行以下代码时,我获得了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();
        }
    }

I add virtual to model as

我将虚拟添加到模型中

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

and I am getting following error to read data 获取Entity框架中多对多关系的ICollection数据

我收到以下错误读取数据

2 个解决方案

#1


0  

You don't need to create the many to many relationship table, Entity framework will do that for you. All you need is to declare your properties as virtual, all use the include method. more details here or here

您不需要创建多对多关系表,Entity框架将为您执行此操作。您只需将属性声明为虚拟属性,所有都使用include方法。更多细节在这里或这里

#2


0  

Make sure to set ICollection method as "virtual" e.g. public virtual ICollection Courses { get; set; }

确保将ICollection方法设置为“虚拟”,例如公共虚拟ICollection课程{get;组; }

Here is an example: http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx

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

Keep in mind if the intersection table contains one or more properties (which are not part of the Primary Key), then don't create an intersection table, just use to ICollection in both entities. However, if there is one or more properties in the intersection, then create the intersection class. This is how the EF Designer works.

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

#1


0  

You don't need to create the many to many relationship table, Entity framework will do that for you. All you need is to declare your properties as virtual, all use the include method. more details here or here

您不需要创建多对多关系表,Entity框架将为您执行此操作。您只需将属性声明为虚拟属性,所有都使用include方法。更多细节在这里或这里

#2


0  

Make sure to set ICollection method as "virtual" e.g. public virtual ICollection Courses { get; set; }

确保将ICollection方法设置为“虚拟”,例如公共虚拟ICollection课程{get;组; }

Here is an example: http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx

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

Keep in mind if the intersection table contains one or more properties (which are not part of the Primary Key), then don't create an intersection table, just use to ICollection in both entities. However, if there is one or more properties in the intersection, then create the intersection class. This is how the EF Designer works.

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