单表一对多关系很常见,它是一种树形结构,如系统菜单表,部门表,分类表,这些都可以做成单表一对多关系,而这些表做成一对多关系后,如果通过EntityFramework进行插入操作时,会很方便,EF会自动为我们确实上级ID,对于操作的性能也是可以接受的,与数据库建立一次链接,发一批指定,这是我们可以接受的,虽然在性能上不是最佳的,但综合对比来看,它即是最佳的。(结合对比=程序性能+开发人员工时+程序扩展性+程序可读性)
OK,我们以菜单表为例,说一下这个菜单表初始化的过程
对于菜单实体的赋值,看一下代码:
var menu = new WebManageMenus { About = string.Empty, LinkUrl = string.Empty, MenuLevel = 0, MenuName = form["MenuName"] ?? "根", Operator = string.Empty, ParentID = null, SortNumber = 1, Status = Status.Normal.GetHashCode(), UpdateDate = DateTime.Now, WebManageMenus1 = new List<WebManageMenus> { new WebManageMenus { About = string.Empty, LinkUrl = string.Empty, MenuLevel = 1, MenuName ="系统管理", Operator = string.Empty, SortNumber = 1, Status = Status.Normal.GetHashCode(), UpdateDate = DateTime.Now, WebManageMenus1 = new List<WebManageMenus> { #region 二级菜单 new WebManageMenus { About = string.Empty, LinkUrl = string.Empty, MenuLevel = 2, MenuName ="角色管理", Operator = string.Empty, SortNumber = 1, Status = Status.Normal.GetHashCode(), UpdateDate = DateTime.Now, WebManageMenus1 = new List<WebManageMenus> { new WebManageMenus { About = string.Empty, LinkUrl = "/Role/Create", MenuLevel = 3, MenuName ="新建角色", Operator = string.Empty, SortNumber = 1, Status = Status.Normal.GetHashCode(), UpdateDate = DateTime.Now }, new WebManageMenus { About = string.Empty, LinkUrl = "/Role/Index", MenuLevel = 3, MenuName ="管理角色", Operator = string.Empty, SortNumber = 1, Status = Status.Normal.GetHashCode(), UpdateDate = DateTime.Now } } }, #endregion #region 二级菜单 new WebManageMenus { About = string.Empty, LinkUrl = string.Empty, MenuLevel = 2, MenuName ="部门管理", Operator = string.Empty, SortNumber = 1, Status = Status.Normal.GetHashCode(), UpdateDate = DateTime.Now, WebManageMenus1 = new List<WebManageMenus> { new WebManageMenus { About = string.Empty, LinkUrl = "/Department/Index", MenuLevel = 3, MenuName ="管理部门", Operator = string.Empty, SortNumber = 1, Status = Status.Normal.GetHashCode(), UpdateDate = DateTime.Now } } }, #endregion #region 二级菜单 new WebManageMenus { About = string.Empty, LinkUrl = string.Empty, MenuLevel = 2, MenuName ="菜单管理", Operator = string.Empty, SortNumber = 1, Status = Status.Normal.GetHashCode(), UpdateDate = DateTime.Now, WebManageMenus1 = new List<WebManageMenus> { new WebManageMenus { About = string.Empty, LinkUrl = "/Menu/Index", MenuLevel = 3, MenuName ="管理菜单管理", Operator = string.Empty, SortNumber = 1, Status = Status.Normal.GetHashCode(), UpdateDate = DateTime.Now } } }, #endregion #region 二级菜单 new WebManageMenus { About = string.Empty, LinkUrl = string.Empty, MenuLevel = 2, MenuName ="员工管理", Operator = string.Empty, SortNumber = 1, Status = Status.Normal.GetHashCode(), UpdateDate = DateTime.Now, WebManageMenus1 = new List<WebManageMenus> { new WebManageMenus { About = string.Empty, LinkUrl = "/User/Create", MenuLevel = 3, MenuName ="新建员工", Operator = string.Empty, SortNumber = 1, Status = Status.Normal.GetHashCode(), UpdateDate = DateTime.Now }, new WebManageMenus { About = string.Empty, LinkUrl = "/User/Index", MenuLevel = 3, MenuName ="管理员工", Operator = string.Empty, SortNumber = 1, Status = Status.Normal.GetHashCode(), UpdateDate = DateTime.Now } } }, #endregion } } } }; MenuRepository.Insert(menu);
我们可以看到,赋值的过程,我采用的EF生成的实体标准,对于子对象的赋值,我们可以看到,parentID并没有赋值,因为这时它还没不存在,这个parentID为上级
菜单的主键ID,所以EF在插入完父菜单后,会自动将ID赋值子对象的ParentID,这个过程是系统帮我们完成的。
对于上面的过程,所生成的SQL语句也是我们可以接受的
感谢您的阅读,感谢Entity Framework开发团队!