自引用

 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. [Android]优化相关

    尽量减少布局的层次,最多10层,可以通过LinearLayout向RelativeLayout的转变来减少层的数量 使用ListView的时候,getView方法中的对象尽量重用

  2. php sleep

    实例一:把时间输出十次,但全部有结果了,才在html浏览器页面输出 ;$i<;$i++){ echo time()."<br>"; sleep(); } echo ...

  3. 阿里云提示:对输入参数id未进行正确类型转义,导致整型注入的发生

    类似以下提示: XXX.php中,对输入参数id未进行正确类型转义,导致整型注入的发生 解决办法: 找到对应文件:$id = $_GET['id']; 增加以下标红过滤: $id = $_GET['i ...

  4. redis中的跳跃表

    参考:http://www.leoox.com/?p=347

  5. RabbitMQ学习

    参考链接:http://www.cnblogs.com/leocook/p/mq_rabbitmq_0.html

  6. 看着水了一天的群,终于看到一段高质量的代码了分享一下localStorage

    _history : { //缓存 isLocalStorage:window.localStorage?true:false, set : function(key,value){ //设置缓存 i ...

  7. hdu2005第几天?

    Problem Description 给定一个日期,输出这个日期是该年的第几天.   Input 输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input , ...

  8. C# Async, Await and using statements

    Async, Await 是基于 .NEt 4.5架构的, 用于处理异步,防止死锁的方法的开始和结束, 提高程序的响应能力.比如: Application area           Support ...

  9. EBS提交请求出现REP-3000错误

    在AIX上利用并发请求提交报表的時候,出现如下错误:REP-3000: Internal error starting Oracle Toolkit.这是因为Report Server需要X-Wind ...

  10. 微信开发(一)内网映射之natapp的使用

    1.https://natapp.cn/client/lists 从官网下载客户端和注册账号 2.打开文件后退出 当前Ctrl+C 输入natapp -authtoken=xxxxx 此为从我的客户端 ...