自连接<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模板引擎,有很强的插件机制,包括支付配送功能都是通过插件来实现的 ...
随机推荐
- MyEclipse:各种提示图标的含义
1.新建的项目,项目名称前有黄色小警示号的原因, 2.项目名称上有红色叹号,说明缺少jar包,仔细找一下,看看缺了什么,添加进去一般就好了. 3.出现红色叉号,那说明代码错误,需要修改代码.
- java对象与XML相互转化
起因 最近在公司做了一次webservice相关的任务,其中我最敢兴趣的就是webservice接受到XML对应的流以后是如何方便的转化成java对象,而java对象又是如何生成对应的XML的. 目的 ...
- 消除类游戏(js版)
最近一直在玩一款消灭星星的消除类游戏,周末无聊就用js也写了一遍,感觉玩比写还困难一直玩不到10000分.废话不多说直接上源码. 效果图(ps 页面有点难看木有美工) 代码总共456行,未经过严格测试 ...
- String StringBuffer StringBuilder
package com.test; import java.util.Date; /*** * * // 输出的结果是:// 来一个测试// 来一个测试如果只输出这句就证明了String是不可变的// ...
- 20155217-杨笛-c与java
命是弱者的借口,运是强者的谦词 你有什么技能比大多数人(超过90%以上)更好? 针对这个技能的获取你有什么成功的经验? 与老师博客中的学习经验有什么共同之处? 其实我经常会去想自己有什么拿得出手的优点 ...
- Google的Protobuf协议分析
protobuf和thrift类似,也是一个序列化的协议实现,简称PB(下文出现的PB代表protobuf). Github:https://github.com/google/protobuf 上图 ...
- jvm--1.class文件结构
1.字节码(1)bytecode是构成平台无关性的基石 (2)当jvm发展到1.7-1.8的时候,jvm设计者通过,JSR-292,基本可以让其他语言运行在jvm上面. 如,Clojure , Gro ...
- Apache 两种mpm prefork 和 worker 的区别
Apache默认的是线程安全的prefork,Prefork MPM 使用多个子进程,每个子进程有一个线程,每个进程每次只处理一个请求连接,一个请求出故障后不会影响其他请求,是线程安全的 worker ...
- SAP模板
用的是kuangbin的模板:http://www.cnblogs.com/kuangbin/archive/2012/09/29/2707955.html ;//点数的最大值 ;//边数的最大值 c ...
- linux 下安装mongodb
1.初始化docker: -v 设置docker和host共享目录,格式hostPath:dockerContainerPath -p 端口映射 --name,容器名称 cen ...