在Entity Framework中以多对多关系插入数据

时间:2015-01-09 16:04:11

标签: c# .net asp.net-mvc linq entity-framework

我在两个表之间有多对多的关系,它们通过在两者之间引入另一个表而中断,包含两者的主键。

enter image description here

这是我的模型类

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_FunctionHierarchy> Navigation_FunctionHierarchy { get; set; }
    public ICollection<Navigation_FunctionInAction> Navigation_FunctionInAction { get; set; }
    public ICollection<Navigation_FunctionInController> Navigation_FunctionInController { get; set; }
}

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_FunctionAction> Navigation_FunctionAction { get; set; }
    public ICollection<Navigation_FunctionInController> Navigation_FunctionInController { get; set; }

}

Navigation_FunctionInController模型

  public Navigation_FunctionInController() 
    { 

    }

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

    public Navigation_FunctionController Navigation_FunctionController { get; set; }
    public Navigation_Functions Navigation_Functions { get; set; }

}

我有CRUD操作的通用存储库

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

现在我有类负责通过调用通用存储库来插入数据并使用数据传递对象,我正在努力插入数据,因为我将两个表的主要插入到Navigation_FunctionInController表中,其中函数和控制器记录同时创建。 ??????????????????????????????????????

我正在阅读博客Insert/Update Many to Many Entity Framework . How do I do it?,但我很难理解如何使用我的结构插入数据。

?????????

    public void CreateFunctionNavigation(FunctionsNavigation_ViewModel _obj)
    {

        using(var _uow = new FunctionsNavigation_UnitOfWork())
        {
            try
            {
                 ??????????????????????

            }
            catch
            {

            }
        }
    }

我的ViewModel

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

    public Navigation_FunctionController _Navigation_FunctionController { get; set; }
}

我已根据建议更新了我的模型,但仍无法在数据库中保存数据,以下是从调试代码中截取的屏幕。

enter image description here

1 个答案:

答案 0 :(得分:2)

据我所知,您在Navigation_Functions和Navigation_FunctionController之间存在多对多的关系。

Navigation_Functions模型应该具有外部实体的属性而不是连接表,如您提供的链接中所建议的那样,您不会映射连接表,因为实体框架会为您处理:

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

应该是

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

然后,当您来保存视图模型时,它将类似于:

public void CreateFunctionNavigation(FunctionsNavigation_ViewModel _obj)
{
    using(var _uow = new FunctionsNavigation_UnitOfWork())
    {
        try
        {
            // UPDATE START
            _obj._Navigation_FunctionController.Navigation_Functions = new List<Navigation_Functions>();
            _obj._Navigation_FunctionController.Navigation_Functions.Add(_obj._Navigation_Functions);
            // UPDATE END

            var navigationFunctions = _obj._Navigation_Functions;
            navigationFunctions.Navigation_FunctionController = new List<Navigation_FunctionController>();
            navigationFunctions.Navigation_FunctionController.Add(_obj._Navigation_FunctionController);

            _uow.Entities.Add(navigationFunctions);
            _uow.Save();
        }
        catch
        {
            // Log exception
        }
    }
}

请注意,我还没有对此代码进行测试,因此您可能需要更新语法。