davin

Just a little thinking and interest!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

在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 2010-12-09 22:12  davin  阅读(843)  评论(0编辑  收藏  举报