自连接<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模板引擎,有很强的插件机制,包括支付配送功能都是通过插件来实现的 ...
随机推荐
- TransMac Win系统下制作 OS X启动盘图文教程超详细小白版
1软件安装好后把准备好的8G或者8G以上U盘插到电脑上:右键以管理员身份运行如 <ignore_js_op> 2打开软件后右键先格式化U盘操作如下图 ...
- APEX初步
APEX是SFDC中用于开发的语言.语法上类似JAVA等面向对象的语言,运行起来类似数据库中的存储过程.可以在SFDC事件中添加业务逻辑,操作相关数据和用在Visual Force页面中. 保存,编译 ...
- [01] cocos2d-x开发环境搭建
cocos2d-x 是跨平台的游戏开发引擎,支持的平台有 ios , android , windows phone , web , tizen,windows等. 先来搭建开发环境,一般我们开发游戏 ...
- Python PIP安装
https://zhidao.baidu.com/question/550936793.html 按图做
- coreseek+sphinx+mysql+thinkphp整合
1.安装coreseek 1.1首先升级或安装系统依赖库 yum install make gcc g++ automake libtool mysql-client libmysqlclient15 ...
- 5天揭秘js高级技术-第一天
一.基础杂记 1. document.write() <script type="text/javascript"> document.write('<h2> ...
- 【IOS】将一组包含中文的数据按照#ABC...Z✿分组
上一篇文章[IOS]模仿windowsphone列表索引控件YFMetroListBox里面 我们一步步的实现了WindowsPhone风格的索引. 但是有没有发现,如果你要实现按照字母排序,你还得自 ...
- CentOS 6.5移除openJDK及JDK安装环境变量配置及JDK版本切换
一.查找已经安装的open JDK [root@localhost ~]# rpm -qa|grep jdk java--openjdk-.el6_3.x86_64 java--openjdk-1.7 ...
- Virus.Win32.Virlock.b分析
0x00 样本说明 分析样本是被0b500d25f645c0b25532c1e3c9741667的样本感染得到.感染前的文件是Tcpview.exe,一款windows网络连接查看工具. 感染前后文件 ...
- Qt工程打包发布
Qt版本 5.7.1 1.添加环境变量 在..\Qt5.7.0\5.7\msvc2013_64\bin(..省略了盘符,例如我的是D:\Qt\Qt5.7.0\5.7\msvc2013_64\bin) ...