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

推荐订阅源

D
DataBreaches.Net
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
B
Blog
博客园 - Franky
I
InfoQ
A
About on SuperTechFans
博客园_首页
L
LangChain Blog
量子位
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
美团技术团队
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
雷峰网
雷峰网
MongoDB | Blog
MongoDB | Blog
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
G
Google Developers Blog
Last Week in AI
Last Week in AI

博客园 - Inrie

[原创]MongoDB、HandlerSocket和MySQL性能测试及其结果分析 [原创]HandlerSocket系列(三):性能及其性能优化 [原创]HandlerSocket系列(二):架构、特点及其应用场景 [原创]HandlerSocket系列(一):由来 SD2.0 2009大会一些感想 在Windows Server 2008集群上做Sql Server 2008集群 解决Azure Project中使用Asp.net MVC RC会让VS崩溃的补丁 [译]剖析ASP.Net MVC Application 让VS 2008也可以用Mobile Web Form 几个不错的WCF Tools ADO.NET Entity Framework Beta3的一些问题 善待你的眼睛-使用微软专为程序员设计的Consolas字体 [推荐]ASP.NET 应用程序的扩展策略 [翻译+推荐]你需要知道的:WCF、WF、ADO.NET SyncServices和ClickOnce Unity Application Block 1.0系列文章 Unity Application Block 1.0系列(7): Lifetime Managers Unity Application Block 1.0系列(6): 杜绝循环引用 Unity Application Block 1.0系列(5): 使用BuildUp让已存在对象实例也支持依赖注入 Unity Application Block 1.0系列(4): 方法调用注入(Method Call Injection )
如何发布Ado.net Entity Framework EDM
Inrie · 2008-04-23 · via 博客园 - Inrie

前言

在开发基于Ado.net Entity Framework的程序时,通常都是把EDM ( 实体数据模型 ) 单独放在的一个Class Library里。

在发布该Class Library时需要注意一些事项,否则可能会出现些问题。

开始

看看下图,解决方案里包括两个Project:EFDemo.Console 和 EFDemo.Models 。这里我把EDM都放在 EFDemo.Models Class Library里。

EF1.jpg
在  EFDemo.Models Class Library 中添加一个"ADO.NET Entity Data Model" 项 ,名为"UserModel"。 通过向导配置连接等操作后就会创建一个EDM。EDM包括三个文件(.csdl、.msl 和.ssdl 文件)。

创建完EDM后会在配置文件(这里是App.Config)的<connectionStrings>节点里添加一些信息。如例子中创建UserModel.edmx后会在配置文件中添加一个子节点:

<connectionStrings>
    <add name="UserEntities" connectionString="metadata=.\UserModel.csdl|.\UserModel.ssdl|.\UserModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=xxxxxx;Initial Catalog=SAASDB;Persist Security Info=True;User ID=sa;Password=xxx;MultipleActiveResultSets=False&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

注意突出显示的“metadata=.\UserModel.csdl|.\UserModel.ssdl|.\UserModel.msl”,在这里指定这三个文件的位置,".\"表示Build后这三个文件会被部署到"Build Output Path"位置。

回到例子中,EFDemo.Console Project 引用 EFDemo.Models Project,这样就可以在 EFDemo.Console Project 中使用 UserModel 了,写完代码后,把上面配置文件信息Copy到EFDemo.Console Project 的App.config中, Ctrl + F5 运行,发现出现下面错误信息:

System.Data.MetadataException: The specified metadata path is not valid. A valid path must be either an existing directory, an existing file with extension '.csdl', '.ssdl', or '.msl', or a URI that identifies an embedded resource.

明显看出是找不到UserModel.csdl、UserModel.ssdl和UserModel.msl这三个文件,到EFDemo.Console的Build Output Path中看确实没有。

有一种解决方法是,每次Build完EFDemo.Models Project后,Copy这三个文件到EFDemo.Console Project的bin里,但是这种做法明显不是最好的。

可以通过以下的配置来更好的解决这问题。

1.打开UserModel Model的属性面板,设置“Metadata Artifact Processing ” 选项的值为“Embed in Output Assembly ”。表示这三个文件会被嵌入到Assembly里。

EF2.jpg

2. 打开UserModel.edmx项的属性面板,确认Build Action值为"EntityDeploy"。

EF3.jpg

以上配置完成之后配置文件中的UserEntities节点值自动改为:

  <connectionStrings>
    <add name="UserEntities" connectionString="metadata=res://*/UserModel.csdl|res://*/UserModel.ssdl|res://*/UserModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=xxxxxx;Initial Catalog=SAASDB;Persist Security Info=True;User ID=sa;Password=xxx;MultipleActiveResultSets=False&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

注意看突出显示的"metadata=res://*/UserModel.csdl|res://*/UserModel.ssdl|res://*/UserModel.msl"。"*"表示它会去所有的Aseembly中找这三个被嵌入在Assembly中的文件。当然如果我们明确知道这三个文件放在EFDemo.Models.dll 这个Assembly中的话,应该指定只在该Assembly中找,而不要浪费时间在所有Assembly中找:

  <connectionStrings>
    <add name="UserEntities" connectionString="metadata=res://EFDemo.Models/UserModel.csdl|res://EFDemo.Models/UserModel.ssdl|res://EFDemo.Models/UserModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=xxxxxx;Initial Catalog=SAASDB;Persist Security Info=True;User ID=sa;Password=xxx;MultipleActiveResultSets=False&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

把这些配置信息更新到EFDemo.Console Project 的App.config中,Ctrl + F5, 运行正常。

作者:Inrie (洪晓军)


出处:http://www.cnblogs.com/inrie