注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

天涯倦客的博客

祝福你朋友永远快乐!

 
 
 

日志

 
 

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

2017-05-22 08:59:49|  分类: asp.net |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
转自:http://www.cnblogs.com/lori/archive/2013/04/02/2995039.html

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

  评论这张
 
阅读(20)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017