EF架构~单表一对多集合的插入(树型结构)

回到目录

单表一对多关系很常见,它是一种树形结构,如系统菜单表,部门表,分类表,这些都可以做成单表一对多关系,而这些表做成一对多关系后,如果通过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开发团队!

回到目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值