在运行时更改架构名称 - 实体框架
我需要在运行时更改实体的存储架构。 我关注了一篇精彩的帖子,可以在这里找到: http://blogs.microsoft.co.il/blogs/idof/archive/2008/08/22/change-entity-framework-storage-db-schema-in-runtime.aspx ?CommentPosted=true#commentmessage
这非常有效,但仅适用于查询,不适用于修改。
知道为什么吗?
I need to change the storage schema of the entities on runtime.
I've followed a wonderful post, available here:
http://blogs.microsoft.co.il/blogs/idof/archive/2008/08/22/change-entity-framework-storage-db-schema-in-runtime.aspx?CommentPosted=true#commentmessage
This works perfectly, but only for queries, not for modifications.
Any idea why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
嗯,我一直在互联网上寻找这段代码。最后我不得不自己做。它基于 Brandon Haynes 适配器,但此函数是您在运行时更改架构所需的全部功能 - 并且您不需要替换自动生成的上下文构造函数。
实例化上下文时,应将生成的 EntityConnection 作为参数传递。你可以修改它,所以所有的ssdl模型都可以通过这个函数修改,而不仅仅是指定的一个。
Well, I was looking for this piece of code all around the Internet. In the end I had to do it myself. It's based on Brandon Haynes adapter, but this function is all you need to change the schema on runtime - and you don't need to replace the autogenerated context constructors.
The resulting EntityConnection should be passed as a parameter when instantiating the context. You can modify it, so all ssdl models are modified by this function, not only the specified one.
我成功地通过使用 CodePlex 中的一个出色的库(由 Brandon Haynes 提供)解决了这个问题,该库名为“Entity Framework Runtime Model Adapter”,可在此处获取:
http://efmodeladapter.codeplex.com/
我对其进行了一些调整,以满足我们的需求,并且没有需要完全替换设计器代码。
所以,我很好。
不管怎样,谢谢,特别是布兰登,干得很棒!
I've managed to resolve this issue by using a brilliant library, located in CodePlex (courtesy of Brandon Haynes), named "Entity Framework Runtime Model Adapter", available here:
http://efmodeladapter.codeplex.com/
I've tweaked it a bit, to fit our needs and without the need of replacing the designer code at all.
So, I'm good.
Thanks anyways, and especially to Brandon, amazing job!
我需要从 postgres 数据库导入数据。它默认使用模式“public”。所以我使用实体框架 CTP 4“代码优先”。它默认使用模式“dbo”。要在运行时更改它,我使用:
它适用于选择、插入、更新和删除数据。所以接下来的测试通过了:
I need import data from postgres database. It by default use schema "public". So I use Entity Framework CTP 4 "Code first". It by default use schema "dbo". To change it in runtime I use:
It work for select, insert, update and delete data. So next test in pass:
本身不是答案,而是 Jan Matousek 的 Create[EntityConnection] 方法的后续内容,展示了如何从 DbContext 使用。注意 DB 是传递给通用存储库的 DbContext 类型。
Not an answer per se but a followup on Jan Matousek's Create[EntityConnection] method showing how to use from a DbContext. Note DB is the DbContext type passed to the generic repository.
我能够将 Jan Matousek 的解决方案转换为使用实体框架 6 在 vb.net 2013 中工作。我还将尝试解释如何在 vb.net 中使用代码。
我们有一个 JD Edwards 数据库,它针对每个环境使用不同的架构(TESTDTA、CRPDTA、PRODDTA)。这使得环境之间的切换变得很麻烦,因为如果您想更改环境,则必须手动修改 .edmx 文件。
第一步是创建一个分部类,它允许您将值传递给实体的构造函数,默认情况下它使用配置文件中的值。
接下来创建将修改内存中的存储架构 .ssdl 文件的函数。
确保 storageNS 命名空间硬编码值与代码中使用的值相匹配,您可以通过调试代码并检查 storageXML 变量来查看实际使用的内容来查看这一点。
现在,您可以在创建实体时在运行时传递新的架构名称和不同的数据库连接信息。不再需要手动更改 .edmx。
这些是使用的 .net 库:
希望可以帮助任何遇到相同问题的人。
I was able to convert the solution from Jan Matousek to work in vb.net 2013 with entity framework 6. I will also try to explain how to use the code in vb.net.
We have a JD Edwards Database which uses different Schema's for each environment (TESTDTA, CRPDTA, PRODDTA). This makes switching between environments cumbersome as you have to manually modify the .edmx file if you want to change environments.
First step is to create a partial class that allows you to pass a value to the constructor of your entities, by default it uses the values from your config file.
Next create the function that will modify your store schema .ssdl file in memory.
Make sure that the storageNS namespace hardcoded value matches the one used in your code, you can view this by debugging the code and examining the storageXML variable to see what was actually used.
Now you can pass a new schema name, and different database connection info at runtime when you create your entities. No more manual .edmx changes required.
These were the .net libraries used:
Hope that helps anyone out there with the same issues.
在将 EF6 与 OData 数据服务结合使用时,我遇到了很多问题,因此我必须找到替代解决方案。就我而言,我真的不需要即时执行此操作。在部署到某些测试环境和安装程序中时,我可以不必更改架构。
使用 Mono.Cecil 重写嵌入的
.ssdl
资源直接在 DLL 中。这对我来说效果很好。以下是如何执行此操作的简化示例:
I had a lot of problems getting this to work when using EF6 with an OData Data Service, so I had to find an alternate solution. In my case, I didn't really need to do it on the fly. I could get away with changing the schema when deploying to some test environments, and in the installer.
Use Mono.Cecil to rewrite the embedded
.ssdl
resources straight in the DLLs. This works just fine in my case.Here is a simplified example of how you can do this: