自连接<EntityFramework6.0>
自引用
public class PictureCategory
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CategoryId { get; private set; }
public string Name { get; set; }
public int? ParentCategoryId { get; private set; }
public virtual PictureCategory ParentCategory { get; set; }
public virtual ICollection<PictureCategory> SubPictureCategories { get; set; } public PictureCategory()
{
SubPictureCategories = new HashSet<PictureCategory>();
}
} public class PictureCategoryContext : DbContext
{
public virtual DbSet<PictureCategory> PictureCategories { get; set; }
public PictureCategoryContext() : base("name=DemoContext") { }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<PictureCategory>()
.HasKey(p=>p.CategoryId)
.HasMany(p => p.SubPictureCategories)
.WithOptional(t => t.ParentCategory)
.HasForeignKey(t=>t.ParentCategoryId);
}
}
怎么使用?
private static void Main()
{
using (var context = new PictureCategoryContext())
{
var _1st = new PictureCategory { Name = "第1代" };
var _2st = new PictureCategory { Name = "第2代" };
var _3_1st = new PictureCategory { Name = "第3_1代" };
var _3_2st = new PictureCategory { Name = "第3_2代" };
var _3_3st = new PictureCategory { Name = "第3_3代" };
var _4st = new PictureCategory { Name = "第4代" };
var _5_1st = new PictureCategory { Name = "第5_5_1代" };
var _5_2st = new PictureCategory { Name = "第5_2代" };
_1st.SubPictureCategories = new List<PictureCategory> { _2st };
_2st.SubPictureCategories = new List<PictureCategory> { _3_1st, _3_2st, _3_3st };
_3_3st.SubPictureCategories = new List<PictureCategory> { _4st };
_4st.SubPictureCategories = new List<PictureCategory> { _5_1st, _5_2st };
context.PictureCategories.Add(_1st);
context.SaveChanges();
} using (var context=new PictureCategoryContext())
{
var query = context.PictureCategories.Where(p=>p.ParentCategory==null).ToList();
query.ForEach(t => Print(t,));
} Console.ReadKey();
} private static void Print(PictureCategory category, int level)
{
Console.WriteLine("{0}--{1}", category.Name, level);
category.SubPictureCategories.ToList().ForEach(t=>Print(t,level+));
}
效果:

模型如下:

再次我们分析一下该关系模型所涉及到degree, multiplicity, and direction:
degree【度】: 一元
multiplicity【复合度,在UML中很常见,也就是重复度】: 0..1和*;因为一个Parent有N个children,而每一个child只能有1个Parent
direction【流向】: 双向
这三个术语详细的介绍看这里
Database relationships are characterized by degree, multiplicity, and direction. Degreeis the number of entity types that participate in the relationship. Unary and binary relationships are the most common. Tertiary and n-place relationships are more theoretical than practical.
Multiplicityis the number of entity types on each end of the relationship. You have seen the multiplicities 0..1 (zero or 1), 1 (one), and * (many).
Finally, the directionis either one-way or bidirectional.
The Entity Data Model supports a particular kind of database relationship called an Association Type.
An Association Type relationship has either unary or binary degree, multiplicities 0..1, 1, or *, and a direction that is bidirectional
自连接<EntityFramework6.0>的更多相关文章
- 分割一个表到多个实体<EntityFramework6.0>
声明方式 public class Photograph { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public in ...
- 继承映射关系 TPH、TPT、TPC<EntityFramework6.0>
每个类型一张表[TPT] 声明方式 public class Business { [Key] public int BusinessId { get; protected set; } public ...
- 将一个实体数据保存到不同的数据表中<EntityFramework6.0>
2014-11-22声明方式 public class Product { [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] public ...
- 多对多关系<EntityFramework6.0>
无负载建立多对多关联的模型 原文中是Modeling a Many-to-Many Relationship with No Payload,虽然这么翻译也有点不准确,但是可以说明其目的,如下图所示, ...
- 使用简介<EntityFramework6.0>
序言 在这一篇中,我们将演示EnitityFramework基本的建模[建模也是EntityFramework最核心的特性]范例,例如实体的分离和继承等.我们开始了演示如何创建一个简单的概念模型的例子 ...
- EntityFramework6.0的Sql读写分离拦截器 和 MVC的 Action拦截器 对比
EF的DbCommandInterceptor类 拦截: EF6.1也出来不少日子了,6.1相比6.0有个很大的特点就是新增了System.Data.Entity.Infrastructure.Int ...
- 升级实体框架EntityFramework6.0
首先安装nuget 管理器 https://visualstudiogallery.msdn.microsoft.com/4ec1526c-4a8c-4a84-b702-b21a8f5293ca 安装 ...
- 序言<EntityFramework6.0>
Entity Framework是微软战略性的数据访问技术,不同与早期访问技术,Entity Framework并不耦合在Visual Studio中,它提供了一个全面的, 基于模型的生态系统,使您能 ...
- Nopcommerce 二次开发0
Nopcommerce 是国外的一个高质量的开源b2c网站系统,基于EntityFramework6.0和MVC5.0,使用Razor模板引擎,有很强的插件机制,包括支付配送功能都是通过插件来实现的 ...
随机推荐
- JavaScript 最简单的图片切换
使用前在文件外部要有1.jpg 2.jpg 只是简单的模仿flash图片切换,可在此基础上引申出各种不同的效果. 思路: 建立一个数组存放图片的src,然后调用setInterval周期性的调用cha ...
- SqlServer批量刷数据执行事务回滚语句备份
企业进行对数据库执行刷数据工作,一段很长的语句希望同时成功或者失败时用到. 1.建立测试环境 /**************************************************** ...
- JavaScript闭包浅谈
------------------- 作者:willingtolove: 本文链接:http://www.cnblogs.com/willingtolove/p/4745889.html 1. 变量 ...
- switch的使用
ji本没用过这个函数,今天用到了它,发现了一些使用要注意的地方: switch的参数支持int和枚举,单jdk1.7后,开始支持String类型.我特意在jdk1.8上试了试, public clas ...
- 配置tomcat系统日志--java eclipse
控制台那里的日志只是部分,有时候报错了我们并没有显示出来,所以需要找到系统日志... 双击tomcat v.80 Service---点击open lauch Configuration--Argum ...
- YII 错误 SQLSTATE[HY000] [2002] No such file or directory
在使用yii的yii\db\Connnection时发生错误 <?php namespace app\controllers; use yii\web\Controller; use yii\d ...
- infoq 微信后台存储架构
infoq 上微信后台存储架构 视频很是值得认真一听,大概内容摘要如下: 主要内容:同城分布式强一致,园区级容灾KV存储系统 - sync 序列号发生器 移动互联网场景下,频繁掉线重连,使用 ...
- git: fatal: Not a git repository (or any of the parent directories): .git
在看书 FlaskWeb开发:基于Python的Web应用开发实战 时,下载完源码后 git clone https://github.com/miguelgrinberg/flasky.git 试着 ...
- C 语言Struct 实现运行类型识别 RTTI
通过RTTI,能够通过基类的指针或引用来检索其所指对象的实际类型.c++通过下面两个操作符提供RTTI. (1)typeid:返回指针或引用所指对象的实际类型. (2)dynamic_cast: ...
- RIDE小技巧——Content Assistance快捷键(CTRL+空格)的修改
大家在用RIDE Content Assistance功能的快捷键时会与机器中是输入法的切换相冲突,这里提供一下修改的位置,大家可以根据个人的喜好修改. 有三处需要修改: {Python_home}\ ...