实体框架 4.1:有没有办法在运行时切换模式
到目前为止(EFv1)我们使用此解决方案: http://efmodeladapter.codeplex.com/ 升级到 .NET Framework 4.0 后,我们正在寻找使用 Entity Framework 4.0/4.1 时在运行时更改架构的内置方法?
更具体地说,我们在数据库上有两个大模式(A 和 B),并且只有模式 A 添加到模型中。 A 和 B 中的表完全相同。 我正在寻找一种在运行时在 A 和 B 之间切换的解决方案。
up to now (EFv1) we used this solution: http://efmodeladapter.codeplex.com/
after upgrading to .NET Framework 4.0 we are looking for built in way to change schema on runtime when using Entity Framework 4.0/4.1?
to be more specific we have two big schemas on DB (A and B) and only schema A added to the Model. the tables in A and B are identically same.
i am looking for a solution to switch between A and B in runtime.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
否。该架构是映射文件的一部分。因此,选项是:
No. The schema is part of mapping file. So the options are either:
我可以分别向您推荐两种型号。并根据条件使用其中一种或另一种。
例如
,如果您的模型有一些相似之处,那么您可以使用某些界面。如果您的模型共享一些通用属性,即它们都有
Category
对象,那么为模型提供两个不同的命名空间就可以解决问题。I can suggest you two models separately. And depending on the conditions use one or another.
For example
If your models have some similarities than you can use some interface. If your models share some commone attributes, i.e. both of them have
Category
objects, then having two different namespaces for models solves the problem.正如 Ladislav 所说,您可以使用两个 SSDL 文件,
以下是操作方法。
使用以下连接字符串初始化 DbContext
元数据=~/bin/Model1.csdl|~/bin/a.ssdl|~/bin/Model1.msl;provider=System.Data.SqlClient;provider连接字符串='数据源=.\SQLEXPRESS;initial Catalog=;integrated security=True;multipleactiveresultsets=True;App=EntityFramework';";
As Ladislav says, You could use two SSDL Files,
Here's how to do it.
Initialize your DbContext using following connection string
metadata=~/bin/Model1.csdl|~/bin/a.ssdl|~/bin/Model1.msl;provider=System.Data.SqlClient;provider connection string='data source=.\SQLEXPRESS;initial catalog=;integrated security=True;multipleactiveresultsets=True;App=EntityFramework';";
这是我对类似场景的看法(与 @Greatran 答案非常相似,但我在 .EDMX 文件上找不到任何元数据工件处理属性项):
的子路径
...\Your\EDMX\Namespace\MyModel.ssdl
调试
环境将是MyModel.Debug.ssdl
嵌入资源
Web.config
文件的字符串。这应该类似于Web.Debug.config
),以便用您的自定义 SSDL 文件替换 EDMX 生成的 SSDL 文件:最后一点可能会变得棘手,至少根据我的经验,因为 EDMX 生成的 SSDL 资源的命名空间与我的自定义 SSDL 资源的命名空间不同。为了获得自定义资源的确切命名空间,我在 VS 中运行了一个调试会话,并在立即窗口中检查了
this.GetType().Assembly.GetManifestResourceNames()
的输出(感谢 stu432)。另外,我更改了
connection string ='AAAAA'
部分,以便匹配部署环境中的数据库服务器。This is my take on a similar scenario (very similar to @Greatran answer, but I could not find any Metadata artifact processing property item on my .EDMX file):
...\Your\EDMX\Namespace\MyModel.ssdl
Debug
environment it would beMyModel.Debug.ssdl
Embedded Resource
Web.config
file. That should be something likeWeb.Debug.config
, in order to substitute the EDMX-generated SSDL file with your custom SSDL file:The last point could get tricky, at least in my experience, as the namespace of EDMX-generated SSDL resource was different from the namespace of my custom SSDL resource. In order to get the exact namespace for my custom resource, I ran a debug session in VS and checked the output of
this.GetType().Assembly.GetManifestResourceNames()
in Immediate Window (thanks to stu432).Also, I changed the
connection string ='AAAAA'
part in order to match the DB server in deploy environment.c# Entity Framework EF 4.1 Change Schema and Database name at runtime 中的解决方案效果很好。它基本上在运行时更改 SSDL 资源中的架构,因此无需为每个使用的架构生成新的 SSDL。
The solution in c# Entity Framework EF 4.1 Change Schema and Database name at runtime works well. It basically changes the schema in the SSDL resource at runtime, so there is no need to generate a new SSDL for each schema used.