惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
V
V2EX
T
The Blog of Author Tim Ferriss
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Hugging Face - Blog
Hugging Face - Blog
Stack Overflow Blog
Stack Overflow Blog
I
InfoQ
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
Recent Announcements
Recent Announcements
Vercel News
Vercel News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
美团技术团队
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks

博客园 - davin

Beginning Asp.Net Security 读书笔记-----XSS phonegap3.0+HTMLl5 开发 ipad app 总结 移动支付-修复FireFox在android移动设备下面的Session 丢失的问题 Window.history.forward(1) 阻止页面后退详解 Pro WPF and Silverlight MVVM:第5章 Event and Command 读书笔记 Pro WPF and Silverlight MVVM 第4章ViewModel 读书笔记 Silverlight4:Devexpress Report Useful rules for compatible with FF,safari and ie8 Entity Framework 4.0 recipes 读书笔记2 ExecuteStoreQuery() Entity Framework 4.0 Recipes 读书笔记1 EDM中的 Complex Type sqlserver2008 + team foundation server 2008 sp1 silverlight animation 读书笔记(4)三角函数 silverlight animation 读书笔记(3)坐标与向量 silverlight animation 读书笔记<2>模糊, 裁剪,拖拽 foundation silverligh3 animation 读书笔记<1>transform 在silverlight中打开调用外部程序的几种方式 Entity Framework object && Json 序列化的问题 silverlight3:(ItemControl 的)UI Virtualization SharpZipLib 数据压缩
Entity Framework 4.0 FK Properties && FK Associat...
davin · 2010-12-09 · via 博客园 - davin

在EF4中, 提供了FK Properties 和 FK Associations的支持,对于association没有了于table mapping的概念,只需要映射到2个实体的属性上.没有FK Properties 的association被称作为Independent Association 。因此最近一周断断续续看了几篇关于FK associations的文章.我不想过多的转述那几篇blog中提到的为何在EF4中做出这样的变化.在EF Design team blog上可以看到许多国外朋友对于FK association的利用与弊的讨论.正因为如此,EF Design team

才采取了一种折中的做法,同时支持FK Properties 和FK association.

下面是我对FK Properties 和FK association所作的练习:

表Department,Course,外键关系

Z)K4OC@FFCJ%BUVMUKU[3PI

%)~OZEB6C$1}3~ZVL%BMICI

department表字段

在EF4.0中

RAPUX26WK}V~9DF$3(]V_PH

生成的EDM:

J6~`HPRCFCW7VMH5(U7%R(B[4] 

image

选中Include foreign key,DepartmentID,选中association,右键看不到table mapping

选中Include foreign key 选中association,右键 还可以看到table mapping

[32~[JY_MUC{AQ6V8VI@DBA

EF4.0还不支持Hierarchyid

双击Assocaition 可以看到下面的窗口,但在vs2008 sp1里面是看不到的,不过在vs2010中选中或不选中include foreign key column in the model

还是有区别的:

3M6@H)TYBG63G[7GKE$WK86

选中Include foreign key

[$3]6K{62)4Y[QIX[5)3PRQ

选中Include foreign key,

因为上图的不同所以,edmx的xml文件也不一样:主要是在csdl中的xml不一样;

选中 include foreign key;

E(YZJER4[SJJ6YCKGY98$8P

不选中 include foreign key;

 

Q3$)S0@T01EOO}35O2_9WDA

 

下面的内容是关于如何在程序中使用FKAssociation,IndependentAssociation

static void InsertDepartmentFKAssociation()
       {
           using (FKAssociation.FKAssociationEntities2 context = new FKAssociation.FKAssociationEntities2())
           {
               FKAssociation.Deparment d = new Deparment {
                   DepartmentID=Guid.NewGuid(),
                   Budget="budget",
                   Name="ist",
                   StartDate=DateTime.Now,
                   Administrator="administrator" };

               FKAssociation.Course c = new Course { 
                   CourseID=Guid.NewGuid(),
                   DepartmentID=d.DepartmentID,
                   Status="status",
                   Title="title" };
               context.AddToDeparments(d);
               context.AddToCourses(c);
               context.SaveChanges();

           }
       }

       static void InsertDepartmentIndependentAssociation()
       {
           using (FKAssociation.FKAssociationEntities2 context = new FKAssociation.FKAssociationEntities2())
           {
               FKAssociation.Deparment d = new Deparment
               {
                   DepartmentID = Guid.NewGuid(),
                   Budget = "budget",
                   Name = "ist",
                   StartDate = DateTime.Now,
                   Administrator = "administrator"
               };

               FKAssociation.Course c = new Course
               {
                   CourseID = Guid.NewGuid(),
                   DepartmentID = d.DepartmentID,
                   Status = "status",
                   Title = "title",
                   Deparment=d,
               };

               d.Courses.Add(c);                          
               context.SaveChanges();

           }
       }

在使用的时候 其实有很多细节可以体现出2者的不同。在一个edm中的Association是可以更据实际情况来决定是否使用IndependentAssociation 还是FKAssociation。

参考文献

http://blogs.msdn.com/efdesign/archive/2008/10/27/foreign-keys-in-the-conceptual-and-object-models.aspx

http://blogs.msdn.com/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx

http://blogs.msdn.com/b/adonet/archive/2009/11/06/foreign-key-relationships-in-the-entity-framework.aspx

http://blogs.msdn.com/b/msdnforum/archive/2010/05/07/foreign-key-association-in-entity-framework-4.aspx

entity framework 4.0 recipes