博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
EF架构~单表一对多集合的插入(树型结构)
阅读量:6257 次
发布时间:2019-06-22

本文共 7872 字,大约阅读时间需要 26 分钟。

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

转载地址:http://exjsa.baihongyu.com/

你可能感兴趣的文章
Apache的安全性,SSL在Solaris 10
查看>>
CentOS 5.11开启VNC访问
查看>>
Mac Homebrew 利器
查看>>
源码安装apache 虚拟主机
查看>>
discuz 数据库密码修改后 管理后台不能登录问题
查看>>
ISA Server 2006简介
查看>>
TCP-IP协议详解(13) DNS协议
查看>>
httpd网站服务
查看>>
mysql启动报错处理
查看>>
4 ways to pass parameter from JSF page to backi...
查看>>
Delphi中获取Unix时间戳及注意事项
查看>>
rvm使用
查看>>
iOS 开发一些小技巧
查看>>
8月5日起OCP电子证书正式推行
查看>>
【原创】DataNode源码演绎 第一回
查看>>
垃圾回收概念与算法
查看>>
JDBC读取MySQL的BLOB类型
查看>>
转帖:Lotus Notes安装和使用的常见问题
查看>>
IconFont 图标svg
查看>>
exchange 2013 邮箱服务器主要服务功能概览
查看>>