简体   繁体   English

实体框架,以多对多关系发布保存数据

[英]Entity framework, issue saving data in many-to-many relationship

I have issue saving data in many-to-may relationship between two tables that breaks by introducing another table in between, containing primary keys of both. 我在以两个表之间的多对多关系保存数据时遇到了问题,该关系通过在两个表之间引入另一个表(包含两个表的主键)而中断。 I have code first existing database approach along with repository pattern and unit of work in MVC application 我有代码优先的现有数据库方法以及MVC应用程序中的存储库模式和工作单元

在此处输入图片说明

and here is my model classes 这是我的模特班

Navigation_Functions 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_FunctionController Model 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; }

}

Junction Model 结模型

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

}

I have generic repository for CRUD operation 我有用于CRUD操作的通用存储库

public void InsertEntity(TEntity obj)
    {
        _DbSet.Add(obj);
    }

My ViewModel 我的ViewModel

public class FunctionsNavigation_ViewModel 
{
    public Navigation_Functions _Navigation_Functions { get; set; }

    public Navigation_FunctionController _Navigation_FunctionController { get; set; }
}

 public void CreateFunctionNavigation(FunctionsNavigation_ViewModel _obj)
  {
    using (var _uow = new FunctionsNavigation_UnitOfWork())
        {
            try
            {                  
                var _navigationFunction = _obj._Navigation_Functions;

               _navigationFunction.Navigation_FunctionController = new List<Navigation_FunctionController>();

               _navigationFunction.Navigation_FunctionController.Add(_obj._Navigation_FunctionController);

               _uow.Navigation_Functions_Repository.InsertEntity(_navigationFunction);

               _uow.Save(); 
            }
            catch
            {

            }
        }
    }

if I remove following line from above code then it save new Navigation_Functions 如果我从上面的代码中删除以下行,则它将保存新的Navigation_Functions

 _navigationFunction.Navigation_FunctionController.Add(_obj._Navigation_FunctionController);

following is screen shot from debug code. 以下是调试代码的屏幕截图。

在此处输入图片说明

I am wondering if my ViewModel are correct? 我想知道我的ViewModel是否正确? secondly How Entity Framework knows that it need to put primary keys of two tables in Navigation_FunctionInController? 其次,实体框架如何知道它需要将两个表的主键放入Navigation_FunctionInController中?

When your model looks like this... 当您的模型看起来像这样...

public class Navigation_Functions
{
    ...
    public ICollection<Navigation_FunctionController> Navigation_FunctionController { get; set; }
}

public class Navigation_FunctionController
{
    ...
    public ICollection <Navigation_Functions> Navigation_Functions { get; set; }
}

...so without a Navigation_FunctionInController class, the junction table in the database ( Navigation_FunctionInController ) is not represented in the class model. ...因此,如果没有Navigation_FunctionInController类,则数据库模型( Navigation_FunctionInController )中的联结表未在类模型中表示。 If you have this model code first, EF will create a junction table itself. 如果您首先拥有此模型代码,EF将自己创建一个联结表。 If you work database first, EF won't create a junction class and in the diagram you will see a pure many to many association, like this: *--* . 如果您首先使用数据库,则EF不会创建结点类,在图中,您将看到纯多对多关联,例如: *--* But this only happens if the junction table only contains the two foreign keys, both of which comprise a compound primary key. 但这仅在联结表仅包含两个外键且两者都包含复合主键的情况下才会发生。

In your model you have an explicit junction class (probably because the table has a primary key field besides the two foreign keys). 在模型中,您具有显式联结类(可能是因为表除了两个外键之外还具有主键字段)。 This means that the many to many association turns into a 1:n:1 association. 这意味着多对多关联变成了1:n:1关联。 The class model should essentially look like this... 类模型本质上应该看起来像这样...

public class NavigationFunction
{
    ...
    public ICollection<NavigationFunctionInController> NavigationFunctionInControllers { get; set; }
}

public class NavigationFunctionController
{
    ...
    public ICollection <NavigationFunctionInController> NavigationFunctionInControllers { get; set; }
}

public class NavigationFunctionInController
{
    public int FunctionInControllerID { get; set; }

    [ForeignKey("NavigationFunction")]
    public int FunctionID { get; set; }

    [ForeignKey("NavigationFunctionController")]
    public int ControllerID { get; set; }

    public NavigationFunctionController NavigationFunctionController { get; set; }
    public NavigationFunction NavigationFunction { get; set; }

}

Note that the foreign key fields don't have to be part of the primary key (also not that I prefer the singular name Navigation_Function and plural names for collections, and no underscores in names). 请注意,外键字段不必一定是主键的一部分(也不是我更喜欢单数名称Navigation_Function和集合的复数名称,名称中不带下划线)。

So what happens in your code is that you have a 1:n:1 association, but you try to manage it like a m:n association by adding items directly to _navigationFunction.Navigation_FunctionController (s) . 因此,代码中发生的事情是您具有1:n:1关联,但是您尝试通过将项直接添加到_navigationFunction.Navigation_FunctionController (s)来像m:n关联一样对其进行管理。 But EF doesn't track that collection, and the items are not saved. 但是EF不会跟踪该集合,并且不会保存项目。

Instead you have to create a junction class instance... 相反,您必须创建一个结点类实例。

var nfic = new NavigationFunctionInController
            {
                NavigationFunction = obj.NavigationFunction,
                NavigationFunctionController = obj.Navigation_FunctionController
            };

...and save it through your repositories and UoW. ...并通过您的存储库和UoW保存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM