EF搭建可扩展菜单
EF实现可扩展性菜单
*:first-child {
margin-top: 0 !important;
}
body>*:last-child {
margin-bottom: 0 !important;
}
/* BLOCKS
=============================================================================*/
p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}
/* HEADERS
=============================================================================*/
h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}
h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}
h1 {
font-size: 28px;
color: #000;
}
h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}
h3 {
font-size: 18px;
}
h4 {
font-size: 16px;
}
h5 {
font-size: 14px;
}
h6 {
color: #777;
font-size: 14px;
}
body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}
a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}
h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}
/* LINKS
=============================================================================*/
a {
color: #4183C4;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
/* LISTS
=============================================================================*/
ul, ol {
padding-left: 30px;
}
ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}
ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}
dl {
padding: 0;
}
dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}
dl dt:first-child {
padding: 0;
}
dl dt>:first-child {
margin-top: 0px;
}
dl dt>:last-child {
margin-bottom: 0px;
}
dl dd {
margin: 0 0 15px;
padding: 0 15px;
}
dl dd>:first-child {
margin-top: 0px;
}
dl dd>:last-child {
margin-bottom: 0px;
}
/* CODE
=============================================================================*/
pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}
code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}
pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}
pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}
pre code, pre tt {
background-color: transparent;
border: none;
}
kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}
/* QUOTES
=============================================================================*/
blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}
blockquote>:first-child {
margin-top: 0px;
}
blockquote>:last-child {
margin-bottom: 0px;
}
/* HORIZONTAL RULES
=============================================================================*/
hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}
/* TABLES
=============================================================================*/
table th {
font-weight: bold;
}
table th, table td {
border: 1px solid #ccc;
padding: 6px 13px;
}
table tr {
border-top: 1px solid #ccc;
background-color: #fff;
}
table tr:nth-child(2n) {
background-color: #f8f8f8;
}
/* IMAGES
=============================================================================*/
img {
max-width: 100%
}
-->
EF搭建可扩展菜单
由于要做一个三级菜单存贮菜单和文章,由于菜单在很多地方用的到,于是想做一个可扩展性的菜单以便以后使用。
由于以前从来没有做过动态的菜单,所以走了很多弯路,尤其搭配EF Code First更是坑了我一把,我想把我碰到坑给大家分享一下。
1.类库的实现
首先我选择树这个数据结构来存贮我的菜单,我定义菜单Menus来作为一个最小单元,定义一个bool类型IsFoot来定义是否为根菜单,每个Menus有一个父级菜单Menus,有一群子菜单,下面是我定义的Menu库。
[Description("菜单")]
public class Menus
{
[Key]
[Display(Name="菜单ID")]
public int MenusID { get; set; }
[Required]
[Display(Name="是否为根节点")]
public bool IsFoot { get; set; }
[Required]
[StringLength(25)]
[Display(Name="目录名字")]
public string Name { get; set; }
[Display(Name="是否删除")]
public bool IsDelete { get; set; }
[Display(Name="包含的文章")]
public virtual List<Article> articles { get; set; }
[Display(Name = "父级菜单")]
public virtual Menus fatherMenus { get; set; }
[Display(Name = "子菜单")]
public virtual List<Menus> sonList { get; set; }
}
每个Menus都包含了一个文章集合,虽然有些菜单不一定有文章但是EF可以允许我们0对多,或1对多。
2. 生成数据库
EF比较人性化的是,当我们数据库里面没有我们想要生成的表时,我们不需要多余的代码,只要当成数据库有表,像平时一样添加数据然后EF会帮我们自动在数据库里面建好表,当然你如果有相同名字的表话它报错,会提醒你数据库里面有如果想保存数据要做好数据迁移工作,数据迁移不是我们的重点,如果想了解的话,点击这里
生成的数据库包含两个表,一个Menus表,一个是Article表(PS:上面没有给出Article的类型定义,想要的可以自己写),对于这个来说,我们并没有在表里面定义外键属性,只是用来一个引用属性,引用属性是一种“虚属性”,我们通过这个属性来建立起两个对象的虚拟联系,比如说父与子,这种关系是虚拟的对于两者之间的联系是通过血缘来联系的,这个血缘是存在的,相对应就是数据库里面的外键联系,外键也可以看做是表中的一个字段,它记录了一种关系。
由于EF的智能关系,当我们Code First时,他会帮我们自动建好外键如果我们不定义的话,当我们使用EF的时候是不需要考虑外键的值的初始化,如果我们没有给他赋值EF会自动给他赋值。
讲完了EF的建立,现在就谈谈使用Code First在项目中遇到的问题。
这个问题主要出在给创建子菜单上,当我们创建子菜单时,我们用的是我们自己的类库代码进行初始化数据库,我们先得到菜单的ID然后在EF里面查询这个菜单,我们查询到这个实体,然后在菜单实体里面添加子菜单,在SaveChange()时候就报错了,EF称检测到有循环赋值的可能,让我们添加外键以避免冲突,我不记得看到的那篇博客看到有人也遇到相同的问题,如果只是普通的一对多(假如是A对1,2,3,4···),当我们给A那个新建一个5时,这个外键的位置是知道的,我们只要在5的外键位置存贮A的主键,然而当我们建立这种父级菜单时,每个菜单里面的外键可以是存贮父级的主键,也可以是子集的主键,所以EF并不能解决冲突,解决这个问题的方法有两种一种是在表中添加外键 如:
[ForeignKey("sonList")]
public int sonListMenusID{get;set;}
或者用Fluent API 在继承的方法 onModelCreate中添加
modelBuilder.Entity<Menus>().HasRequired(p=>p.sonList).WithMany(l=>l.Menus).HasForeignKey(p=>p.sonListMenusID)
通过这种创建方式当我们创建子集菜单时我们就可以成功利用EF特性帮我们自动添加上外键,以及建立好实体关系。
关于更详细的外键知识可以点击这里
EF搭建可扩展菜单的更多相关文章
- SharePoint 2013 自定义扩展菜单
在对SharePoint进行开发或者功能扩展的时候,经常需要对一些默认的菜单进行扩展,以使我们开发的东西更适合SharePoint本身的样式.SharePoint的各种功能菜单,像网站设置.Ribbo ...
- SharePoint 2013 自定义扩展菜单(二)
接博文<SharePoint 2013 自定义扩展菜单>,多加了几个例子,方便大家理解. 例七 列表设置菜单扩展(listedit.aspx) 扩展效果 XML描述 <CustomA ...
- 如何搭建redis扩展-Yii中文网
原文链接: 如何搭建redis扩展http://www.yii-china.com/post/detail/43.html 安装redis扩展: 1.通过composer进行安装,到项目根目录cmd运 ...
- swoole新手教程01-环境搭建及扩展安装
写在前面的废话 <swoole源代码分析>已经写了13章,整个swoole的核心架构基本都分析的差点儿相同了.于是心里一直以来想整理swoole的文档并写一份教程的想法就再度浮了出来. 实 ...
- VS+mysql+EF搭建
2016年7月6日更新: vs2010只需要安装mysql的.net connector就可以 vs2012, vs2015都需要安装.net connector + ODBC connector才行 ...
- Code First开发系列实战之使用EF搭建小型博客平台
返回<8天掌握EF的Code First开发>总目录 本篇目录 理解应用需求 数据库设计 创建实体数据模型 创建实体类 创建关系和导航属性 实现DbContext类 执行数据访问 理解仓储 ...
- 使用vs2008搭建php扩展环境
所需要的工具或者文件: 1.php源码,去官网下载即可.http://windows.php.net/download 2.php-sdk:php 开发工具包 3.deps类库 4.Cygwin:wi ...
- Linux 搭建php扩展开发框架
1.安装phpize(如果是使用php源码编译就免了,本身就有) 2.打开php源码,ext中有ext_skel工具,使用它可以方便 ./ext_skel --extname = myext 生成扩展 ...
- 使用Asp.Net Core MVC 开发项目实践[第四篇:基于EF Core的扩展2]
上篇我们说到了基于EFCore的基础扩展,这篇我们讲解下基于实体结合拉姆达表达式的自定义更新以及删除数据. 先说下原理:其实通过实体以及拉姆达表达式生成SQL语句去执行 第一种更新扩展: 自定义更新字 ...
随机推荐
- scala中的implict
1.作为隐式参数 object Test { def main(args: Array[String]) { import FruitColor._ Fruit.get("apple&quo ...
- UVa10651(记忆化搜索)
题意: 给一串-和o组成的字符串,你可以把“-oo"变成”o--",可以把“oo-”变成“--o",问最后最少有多少个o. 状态压缩 ,记忆化搜索 code: #incl ...
- Oracle11g R2学习系列 之十 解决EM不能用
不知道是什么原因https://localhost:1158/em,今天突然就不能用了.做了好多搜索也没有解决.现象是在services.msc中,不能重启OracleDBConsole服务,提示: ...
- ashx中session的使用
在平常的页面上是用是很容易就的到request,response对像,从而对其进行一些操作,但在ashx(一般处理程序)中却是有一点的不同, 在ashx你无法正常的使用session,即 contex ...
- 02—从Cocos2DX视角看游戏组成
Cocos2DX引擎按照层次逻辑将游戏元素细分为:导演CCDirector.场景CCScene.图层CCLayer.精灵CCSprite.在上面篇中我们已经知道除导演CCDirector外都继承了CC ...
- 在JasperReport中填充JavaBean(4)
使用Parameters参数对象传递字符串的示例,本节将演示打印List接口中Userinfo.java实体类的示例,打印的数据源不是来自于Parameters对象,而是JRBeanCollectio ...
- 【转】嵌入式Linux学习笔记
一 嵌入式系统定义: 应用于特定环境的硬件体系. 二 两样非常重要的能力: 1. 掌握各种新概念的能力 2. 调试的能力( 包括软件, 硬件 ) 三 需要的基础知识: 1. 操作系统理论基 ...
- C语言复习--实现栈
C #include <stdio.h> #include <stdlib.h> #define STACK_SIZE 100 typedef char TYPE; typed ...
- man手册导出成txt,pdf,html的一些小技巧
经常man一些shell命令,有时候有想导出来编辑或注释一下,所以要导出.方法有很多种,根据自己的实际需要觉得比较实用的记录下分享一下. 1.导出成txt man –t bash |col –b &g ...
- hdu 1142 A Walk Through the Forest
http://acm.hdu.edu.cn/showproblem.php?pid=1142 这道题是spfa求最短路,然后dfs()求路径数. #include <cstdio> #in ...