为什么实体框架连接需要元数据属性?

发布于 2024-07-12 04:03:29 字数 911 浏览 8 评论 0原文

我将 DAL 从使用 LINQ 切换到实体框架。 由于我的应用程序根据当前用户连接到不同的数据库,因此我需要在运行时动态创建 DataContext 并传入适当的连接字符串。 但是,当我尝试使用旧连接字符串以编程方式创建实体框架连接时,连接失败。 它抱怨它无法识别连接字符串中的密钥,确切地说是“服务器”。

我发现我需要这样做才能使实体框架连接正常工作:

EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = "System.Data.SqlClient";
entityBuilder.ProviderConnectionString = clientConnectionString;
entityBuilder.Metadata = "res://*/xxxxxxxxxx.csdl...";
Entities entities = new Entities(entityBuilder.ToString());

这是为什么?
元数据属性有什么用?
对于多个不同的连接来说,它总是相同的,这会是一个问题吗?
应该是什么?
有没有办法解决?

提前致谢!

更新1: 感谢兰多夫的更新,但是...
我遇到此问题的全部原因是我无法将连接字符串存储在配置文件中。 连接字符串是在运行时动态确定用户连接的。

这是我的具体场景:
如果用户 A 正在连接,则应用程序从数据库 A 中提取数据。如果用户 B 正在连接,则应用程序从数据库 B 中提取数据。
连接字符串存储在主数据库中,并且数量可能是无限的。 每次添加用户时,我都不想进入 web.config,更不用说它最终会变得巨大!

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.

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.

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲凉≈ 2024-07-19 04:03:29

扩展 Randolpho 的答案:

元数据属性专门指向 .SSDL(存储模型)、.CSDL(概念模型)和 .MSL(映射模型)文件的位置。 这三个文件本质上是实体数据模型。 “res://”URI 样式限定符指示文件作为资源嵌入到已编译的 EDM 程序集中。

Expanding on Randolpho's answer:

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.

深海里的那抹蓝 2024-07-19 04:03:29

您会发现这些链接信息非常丰富:

http:// /msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnection.connectionstring.aspx

http://weblogs.asp.net/pgielens/archive/2006/08/21/ADO.NET-Entity-Framework-Metadata .aspx

底线? 实体框架需要元数据来构建实体映射。

此外,您应该考虑将连接信息移至配置文件中,而不是在代码中构建它。 第一个链接将向您展示如何执行此操作。

You will find these links very informative:

http://msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnection.connectionstring.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文