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

推荐订阅源

V
Vulnerabilities – Threatpost
U
Unit 42
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
F
Full Disclosure
月光博客
月光博客
Engineering at Meta
Engineering at Meta
博客园_首页
The Register - Security
The Register - Security
G
Google Developers Blog
The Cloudflare Blog
博客园 - Franky
K
Kaspersky official blog
A
Arctic Wolf
Scott Helme
Scott Helme
C
Cisco Blogs
Hugging Face - Blog
Hugging Face - Blog
C
Check Point Blog
NISL@THU
NISL@THU
AI
AI
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
Vercel News
Vercel News
T
Tor Project blog
P
Privacy International News Feed
D
Docker
I
Intezer
L
LangChain Blog
P
Proofpoint News Feed
Security Latest
Security Latest
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
博客园 - 聂微东
AWS News Blog
AWS News Blog
Martin Fowler
Martin Fowler
P
Privacy & Cybersecurity Law Blog
V
V2EX
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
The Hacker News
The Hacker News
T
Tenable Blog
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog

博客园 - 张剑

WebMatrix&Razor建站系列之WebMatrix介绍 Windows Phone 7 XNA开发之关于游戏组件 Windows Phone 7 XNA开发之关于图形的配置 vs2010中添加项目中找不到EntityFramework实体框架解决办法 使用MSDN学习ASP.NET的工作流程 Windows Phone 7、XNA的旋转的背景 《XNA游戏开发》在战机游戏中使用Decorator模式 不被重视的基础,简单高效地使用ADO.net连接对象 微软2011 GCR MVP Open Day 之旅! ASP.NET4.5与VisualStudio11预览 程序员杂记系列文章,30岁之前的回忆。 程序员杂记:带面具的生活! MVC中在路由表routes集合中添加Route实例的一些问题。 Windows Phone 7之HelloWorld! MVC3+Entity Framework 实现投票系统(三) MVC3+Entity Framework 实现投票系统(二) MVC3+Entity Framework 实现投票系统(一) 关于Windows Phone 7开发工具离线安装包 程序员杂记:我们的爱情故事
EntityFramework外健的读写
张剑 · 2012-03-18 · via 博客园 - 张剑

2012-03-18 11:10  张剑  阅读(447)  评论()    收藏  举报

1.首先创建新闻表与新闻分类表,并建立主外键关系如下:

(注意:EF在使用时,表是需要定义主键的)

2.在VS2010的项目中点右建添加实体,选择到指定数据库,在以前的文章中有:http://jianle.blog.51cto.com/429692/723758

(注意:添加后可以看到NEWS实体中外键属性不存在了,变成了导航属性)

3.添加外键时使用的方法:

  1. publicbool AddNews(string title, int classid, string content,DateTime date,int hot )
  2. {
  3. bool mark = false;
  4. using (DemosModel.DemosEntities dde = new DemosModel.DemosEntities())
  5. {
  6. try
  7. {
  8. var nc = dde.NewsClass.First(p => p.id == classid);
  9. DemosModel.News dn = new DemosModel.News();
  10. dn.NewsClass = nc;
  11. dn.NewsTitle = title;
  12. dn.NewsContent = content;
  13. dn.NewsDate = date;
  14. dn.NewsHot = hot;
  15. dde.AddToNews(dn);
  16. dde.SaveChanges();
  17. mark = true;
  18. }
  19. catch (Exception err)
  20. {
  21. FileManage.Instance.AddLog(err.Message);
  22. }
  23. }
  24. return mark;
  25. }

要进行写入外健的操作,必须得先将外键表中对应的对象得到:

  1. var nc = dde.NewsClass.First(p => p.id == classid);

然后在为news实体对象的NewsClass导航属性赋值为查询出的外键表对象:

  1. DemosModel.News dn = new DemosModel.News();
  2. dn.NewsClass = nc;

最后添加保存就可以了:

  1. dde.AddToNews(dn);
  2. dde.SaveChanges();

4.查询News表时,获取外键表的方法:

  1. public List<DemosModel.News> NewsList()
  2. {
  3. List<DemosModel.News> list = new List<DemosModel.News>();
  4. using (DemosModel.DemosEntities dde = new DemosModel.DemosEntities())
  5. {
  6. list = dde.News.Include("NewsClass").ToList<DemosModel.News>();
  7. }
  8. return list;
  9. }

在查询News表时,需要使用Include方法将外键表包括进来,否则查询的结果中则外键表导航属性为null值。

  1. dde.News.Include("NewsClass");