博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自连接<EntityFramework6.0>
阅读量:5904 次
发布时间:2019-06-19

本文共 3722 字,大约阅读时间需要 12 分钟。

 自引用

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
SubPictureCategories { get; set; } public PictureCategory() { SubPictureCategories = new HashSet
(); } } public class PictureCategoryContext : DbContext { public virtual DbSet
PictureCategories { get; set; } public PictureCategoryContext() : base("name=DemoContext") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity
() .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
{ _2st }; _2st.SubPictureCategories = new List
{ _3_1st, _3_2st, _3_3st }; _3_3st.SubPictureCategories = new List
{ _4st }; _4st.SubPictureCategories = new List
{ _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,1)); } 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+1)); }

效果:

模型如下:

 

 

再次我们分析一下该关系模型所涉及到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

转载于:https://www.cnblogs.com/rohelm/p/4109549.html

你可能感兴趣的文章
错误请联系管理员文件 index.php,帝国CMS订单、反馈信息、投稿与留言发邮件通知管理员的方法...
查看>>
小米笔记本装linux教程视频教程,Archlinux安装指南~小米笔记本Air 13.3英寸版本
查看>>
linux卸载nomachine,NoMachine 安装与配置及使用
查看>>
企业shell常见面试题及企业实战案例深入浅出讲解
查看>>
Load Test
查看>>
美文共赏
查看>>
RHEL6入门系列之十七,打包与压缩
查看>>
SQLite 3.7.13的加密解密(二)—— 开放宏定义
查看>>
禁止server 2008域端口的脚本
查看>>
数据结构图之二(最小生成树--普里姆算法)
查看>>
HTML输出 一 控制列背景颜色
查看>>
Redis for Windows(C#缓存)配置文件详解
查看>>
回忆2013年的点点滴滴(各个方面)
查看>>
ASP.NET MVC 4使用PagedList.Mvc分页
查看>>
HDOJ 2066 floyed优化算法
查看>>
window.onscroll
查看>>
开发常用动画收集
查看>>
在 Ubuntu 14.04 上安装 Ubuntu Tweak 0.8.8
查看>>
Dynamic CRM 2015学习笔记 系列汇总
查看>>
Android学习之路
查看>>