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

推荐订阅源

P
Palo Alto Networks Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
S
Schneier on Security
S
Securelist
酷 壳 – CoolShell
酷 壳 – CoolShell
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cyberwarzone
Cyberwarzone
Apple Machine Learning Research
Apple Machine Learning Research
S
SegmentFault 最新的问题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
GbyAI
GbyAI
Security Latest
Security Latest
Last Week in AI
Last Week in AI
Microsoft Security Blog
Microsoft Security Blog
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
Webroot Blog
Webroot Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
C
Cisco Blogs
博客园 - 【当耐特】
Blog — PlanetScale
Blog — PlanetScale
Hugging Face - Blog
Hugging Face - Blog
B
Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Attack and Defense Labs
Attack and Defense Labs
The Last Watchdog
The Last Watchdog
U
Unit 42
阮一峰的网络日志
阮一峰的网络日志
Project Zero
Project Zero
WordPress大学
WordPress大学
L
LINUX DO - 最新话题
F
Fortinet All Blogs
L
LINUX DO - 热门话题
PCI Perspectives
PCI Perspectives
Simon Willison's Weblog
Simon Willison's Weblog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
MongoDB | Blog
MongoDB | Blog
Latest news
Latest news
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
The Hacker News
The Hacker News
爱范儿
爱范儿
O
OpenAI News
J
Java Code Geeks
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - Henry Cui

招聘高级.Net工程师 Node.js Web开发(二)认识Express(上) Node.js Web开发(一)从零开始 Html5 Step by Step(二) 本地存储 HTML 5 Step by Step(一) 拖放API Git学习笔记(一)初识Git DDD中的Specification模式 Silverlight 4之旅(三)数据绑定(中) Silverlight 4之旅(二)数据绑定(上) Silverlight 4之旅(一) .Net 4.0 Parallel 编程之旅 .Net 4.0 Parallel 编程(九)Task中的数据共享(下) .Net 4.0 Parallel编程(八)Task中的数据共享(中) .Net 4.0 Parallel 编程(七)Task中的数据共享(上) .Net 4.0 Parallel 编程(六)Task(下) .Net 4.0 Parallel 编程(五)Task(中) .Net 4.0 Parallel 编程(四) Task(上) Entity Framework Code-First(下) Entity Framework Code-First(上)
Entity Framework POCO T4模板使用
Henry Cui · 2010-07-23 · via 博客园 - Henry Cui

2010-07-23 22:41  Henry Cui  阅读(5375)  评论()    收藏  举报

在之前的文章中写过Entity Framework中POCO的支持。其实在VS2010的Extension里面提供了POCO的模板给我们了,我们可以很快地来实现Entity 跟Contenx的生成了。本文中将介绍POCO模板的使用。

安装

image

我们选择C#POCO Entity Generator,下载之后,在弹出的对话框中选择安装即可。

生成代码

在这里我们使用Northwind数据库,首先先建立了一个控制台项目,然后添加NorthdWind的edmx,别忘记了吧Custom Tool设置为空,因为我们要使用POCO的功能:

image

在新建项中我们选择POCO Etntiy Generator:

image

在确定之后我们会看到如下错误:

image

这是因为模板中找不到inputfile的原因,在模板里面我们找到InputFile:

image

修改为:Northwind.edmx之后我们可以看到模板就会帮我们把对应的实体跟Contenxt全部生成好了:

image

同时我们可以设置生成代码的命名空间:

image

生成的实体中会包含:原始属性、导航属性以及关系的Fixup。

分解到另外项目中与查询

新建一个Northiwind.Entities,我们可以直接把Northwind.tt移到Entities项目中去,被忘了同时需要修改InputFileName为:@"..\\Northwind.ConsoleApp\\Northwind.edmx"

我们看到解决方案结构:

image

我们虚幻要将Northowind.Context.tt的Input的FileName设置好,以及添加对Northiwind.Entities的引用。下面我们来编写查询试试看:

            using (var context = new NorthwindEntities())
            {
                var products = from product in context.Products
                               where product.CategoryID == 1
                               select product;
                foreach (var product in products)
                {
                    Console.WriteLine("The Product Info : ProductName:{0} CategoryID:{1}",
                        product.ProductName,
                        product.CategoryID);
                }
                Console.Read();
            }

结果:

image

总结

在本文中介绍使用了T4模板为我们生成POCO的实体的示例,我们可以看到是如此的方便简单。这样的事我们好像在以前在使用NHibernate时,使用Codesmith生成代码一样。