自引用

 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>的更多相关文章

  1. 分割一个表到多个实体<EntityFramework6.0>

    声明方式 public class Photograph { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public in ...

  2. 继承映射关系 TPH、TPT、TPC<EntityFramework6.0>

    每个类型一张表[TPT] 声明方式 public class Business { [Key] public int BusinessId { get; protected set; } public ...

  3. 将一个实体数据保存到不同的数据表中<EntityFramework6.0>

    2014-11-22声明方式 public class Product { [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] public ...

  4. 多对多关系<EntityFramework6.0>

    无负载建立多对多关联的模型 原文中是Modeling a Many-to-Many Relationship with No Payload,虽然这么翻译也有点不准确,但是可以说明其目的,如下图所示, ...

  5. 使用简介<EntityFramework6.0>

    序言 在这一篇中,我们将演示EnitityFramework基本的建模[建模也是EntityFramework最核心的特性]范例,例如实体的分离和继承等.我们开始了演示如何创建一个简单的概念模型的例子 ...

  6. EntityFramework6.0的Sql读写分离拦截器 和 MVC的 Action拦截器 对比

    EF的DbCommandInterceptor类 拦截: EF6.1也出来不少日子了,6.1相比6.0有个很大的特点就是新增了System.Data.Entity.Infrastructure.Int ...

  7. 升级实体框架EntityFramework6.0

    首先安装nuget 管理器 https://visualstudiogallery.msdn.microsoft.com/4ec1526c-4a8c-4a84-b702-b21a8f5293ca 安装 ...

  8. 序言<EntityFramework6.0>

    Entity Framework是微软战略性的数据访问技术,不同与早期访问技术,Entity Framework并不耦合在Visual Studio中,它提供了一个全面的, 基于模型的生态系统,使您能 ...

  9. Nopcommerce 二次开发0

    Nopcommerce  是国外的一个高质量的开源b2c网站系统,基于EntityFramework6.0和MVC5.0,使用Razor模板引擎,有很强的插件机制,包括支付配送功能都是通过插件来实现的 ...

随机推荐

  1. 使用django开发博客过程记录5——日期归档和视图重写

    针对每条博客的观看次数我么是使用django的Mixin实现的: def get(self, request, *args, **kwargs): last_visit = request.sessi ...

  2. java内存区域简介

    运行时数据区域 1.程序计数器:是一块较小的内存空间,可以看做当前线程所执行的字节码的行号指示器.字节码解释器工作时就是通过改变计数器的值来选取下一条需要执行的字节码指令,分支.循环.跳转.异常处理. ...

  3. jsp标签

    常规的jsp标签,导入如下 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Monaco; color: #3933ff } span.s1 ...

  4. Oracle TO_DATE 日期格式

    Oracle trunc()函数的用法 --Oracle trunc()函数的用法/**************日期********************/1.select trunc(sysdat ...

  5. Android 项目结构图

    src:存放Java源代码 gen:存放系统自动生成的配置文件 Android 4.4.2:包含Android.jar文件,包含构建应用程序所需的所有Android SDK库 asssets:存放资源 ...

  6. jQuery sibings()的作用

    jQuery sibings()的作用: siblings() 获得匹配集合中每个元素的同胞,通过选择器进行筛选是可选的. 当我们要对一个<li></li>列表的操作的时候,只 ...

  7. LUA中将未分类数据分为测试集和训练集

    require 'torch' require 'image' local setting = {parent_root = '/home/pxu/image'} function list_chil ...

  8. Sublime Text3插件管理

    插件安装 package control 安装Sublime Text3 打开Sublime Text3,Ctrl+~ 调出控制台,输入代码安装 package control 代码如下: impor ...

  9. 一些有趣的Javascript技巧

    整理一些刷题时学会的小技巧…… 目录: 即大于0又小于0的变量 String.split() 与 正则表达式 缓存的几种方法 初始化一个数组 即大于0又小于0的变量 问题: 设计一个变量val,使得以 ...

  10. 使用spring的AOP时产生的异常

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' ...