database 为什么实体框架连接需要元数据属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/496856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does an Entity Framework Connection require a metadata property?
提问by John Bubriski
I switched my DAL from using LINQ over to Entity Framework. Because my application connects to different databases depending on the current user, I need to dynamically create the DataContext at run time and pass in the appropriate connection string. However, when I tried to programatically create an Entity Framework connection using my old connection string, the connection failed. It complained that it didn't recognize the key in the connection string, "server" to be exact.
我将我的 DAL 从使用 LINQ 切换到实体框架。因为我的应用程序根据当前用户连接到不同的数据库,所以我需要在运行时动态创建 DataContext 并传入适当的连接字符串。但是,当我尝试使用旧连接字符串以编程方式创建实体框架连接时,连接失败。它抱怨说它无法识别连接字符串中的键,确切地说是“服务器”。
I found out that I needed to do this in order to get the Entity Framework connection to work:
我发现我需要这样做才能使实体框架连接正常工作:
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = "System.Data.SqlClient";
entityBuilder.ProviderConnectionString = clientConnectionString;
entityBuilder.Metadata = "res://*/xxxxxxxxxx.csdl...";
Entities entities = new Entities(entityBuilder.ToString());
Why is this?
What is the Metadata property for?
Is it going to be a problem that its always the same for multiple different connections?
What should it be?
Is there any way around this?
为什么是这样?
元数据属性有什么用?
对于多个不同的连接,它总是相同的会是一个问题吗?
应该是什么?
有没有办法解决?
Thanks in advance!
提前致谢!
Update 1:
Thanks for the update Randolpho, but...
The whole reason I'm having this issue, is that I can't store the connection strings in a configuration file. The connection string is dynamically determined at runtime by which user is connecting.
更新 1:感谢 Randolpho 的更新,但是...
我遇到这个问题的全部原因是我无法将连接字符串存储在配置文件中。连接字符串在运行时由用户连接动态确定。
Here is my exact scenario:
If user A is connecting, the app pulls data from database A. If user B is connecting, the app pulls data from database B.
The connection strings are stored in a main database, and the number is potentially limitless. Every time I add a user, I don't want to have to go into the web.config, not to mention the fact that it would eventually get HUGE!
这是我的确切场景:
如果用户 A 正在连接,应用程序从数据库 A 中提取数据。如果用户 B 正在连接,应用程序从数据库 B 中提取数据。
连接字符串存储在主数据库中,并且数量可能是无限的. 每次添加用户时,我都不想进入 web.config,更不用说它最终会变得巨大的事实!
采纳答案by Randolpho
You will find these links very informative:
您会发现这些链接非常有用:
http://weblogs.asp.net/pgielens/archive/2006/08/21/ADO.NET-Entity-Framework-Metadata.aspx
http://weblogs.asp.net/pgielens/archive/2006/08/21/ADO.NET-Entity-Framework-Metadata.aspx
Bottom line? Entity Framework needs the metadata to build your entity mappings.
底线?实体框架需要元数据来构建实体映射。
Additionally, you should consider moving your connection information out to your configuration file rather than build it in code. The first link will show you how to do that.
此外,您应该考虑将连接信息移到配置文件中,而不是在代码中构建它。第一个链接将向您展示如何做到这一点。
回答by Dave Swersky
Expanding on Randolpho's answer:
扩展 Randolpho 的回答:
The metadata property specifically points to the location of the .SSDL (Storage Model,) .CSDL (Conceptual Model,) and .MSL (Mapping Model) files. These three files essentially are the Entity Data Model. The "res://" URI-style qualifier indicates that the files are embedded as resources in the compiled EDM assembly.
元数据属性专门指向 .SSDL(存储模型)、.CSDL(概念模型)和 .MSL(映射模型)文件的位置。这三个文件本质上是实体数据模型。“res://” URI 样式限定符表示文件作为资源嵌入到已编译的 EDM 程序集中。