Entity Framework 4.0 FK Properties && FK Associations

在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,外键关系

Entity Framework 4.0 FK Properties && FK Associations_第1张图片

Entity Framework 4.0 FK Properties && FK Associations_第2张图片

department表字段

在EF4.0中

Entity Framework 4.0 FK Properties && FK Associations_第3张图片

生成的EDM:

Entity Framework 4.0 FK Properties && FK Associations_第4张图片 

Entity Framework 4.0 FK Properties && FK Associations_第5张图片

选中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

还是有区别的:

Entity Framework 4.0 FK Properties && FK Associations_第6张图片

选中Include foreign key

Entity Framework 4.0 FK Properties && FK Associations_第7张图片

选中Include foreign key,

 

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

选中 include foreign key;

Entity Framework 4.0 FK Properties && FK Associations_第8张图片

不选中 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 

 

你可能感兴趣的:(association)