davin

Just a little thinking and interest!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  53 随笔 :: 8 文章 :: 127 评论 :: 13万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

在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 

 

posted on   davin  阅读(847)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示