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

推荐订阅源

Google Online Security Blog
Google Online Security Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
Cisco Talos Blog
Cisco Talos Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Scott Helme
Scott Helme
Project Zero
Project Zero
E
Exploit-DB.com RSS Feed
S
Secure Thoughts
K
Kaspersky official blog
L
Lohrmann on Cybersecurity
NISL@THU
NISL@THU
WordPress大学
WordPress大学
N
News and Events Feed by Topic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LINUX DO - 热门话题
小众软件
小众软件
P
Privacy & Cybersecurity Law Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
A
About on SuperTechFans
Hacker News: Ask HN
Hacker News: Ask HN
AWS News Blog
AWS News Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hacker News: Front Page
F
Full Disclosure
Latest news
Latest news
Schneier on Security
Schneier on Security
The Hacker News
The Hacker News
T
Troy Hunt's Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Jina AI
Jina AI
Martin Fowler
Martin Fowler
P
Proofpoint News Feed
TaoSecurity Blog
TaoSecurity Blog
G
GRAHAM CLULEY
Forbes - Security
Forbes - Security
V
V2EX - 技术
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Vulnerabilities – Threatpost
C
Cyber Attacks, Cyber Crime and Cyber Security
MongoDB | Blog
MongoDB | Blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
P
Privacy International News Feed
C
Check Point Blog
N
News and Events Feed by Topic

博客园 - 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 Associations
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