实体框架 4.1:有没有办法在运行时切换模式

发布于 2024-12-14 20:29:02 字数 301 浏览 1 评论 0原文

到目前为止(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 技术交流群。

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

发布评论

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

评论(5

穿越时光隧道 2024-12-21 20:29:02

否。该架构是映射文件的一部分。因此,选项是:

  • 不要使用映射文件作为资源。在运行时更改 SSDL 文件。
  • 使用两个不同的 SSDL 文件并在运行时构建连接字符串。

No. The schema is part of mapping file. So the options are either:

  • Don't use mapping files as resources. Change the SSDL file at runtime.
  • Use two different SSDL files and build connection string at runtime.
红ご颜醉 2024-12-21 20:29:02

我可以分别向您推荐两种型号。并根据条件使用其中一种或另一种。
例如

void Foo(DbContext model) {
    if (model.GetType() == typeof(FooNamespace.MyFooModel)){
        var db = model as FooNamespace.MyFooModel;
        //Do stuff
    } else {
        var db = model as BarNamespace.MyBarModel;
        //Do stuff
    }
}

,如果您的模型有一些相似之处,那么您可以使用某些界面。如果您的模型共享一些通用属性,即它们都有 Category 对象,那么为模型提供两个不同的命名空间就可以解决问题。

I can suggest you two models separately. And depending on the conditions use one or another.
For example

void Foo(DbContext model) {
    if (model.GetType() == typeof(FooNamespace.MyFooModel)){
        var db = model as FooNamespace.MyFooModel;
        //Do stuff
    } else {
        var db = model as BarNamespace.MyBarModel;
        //Do stuff
    }
}

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.

献世佛 2024-12-21 20:29:02

正如 Ladislav 所说,您可以使用两个 SSDL 文件,

以下是操作方法。

  • 右键单击您的 .edmx 文件并选择属性,
  • 将“元数据工件处理”更改为复制到输出目录
  • 从输出目录中打开 .ssdl 文件
  • 替换所有Schema="dbo" Schema="A"
  • 将文件另存为 a.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.

  • Right click on your .edmx file and choose properties
  • change "Metadata artifact Processing" to Copy to Output Directory
  • open .ssdl file from your output directory
  • replace all Schema="dbo" to Schema="A"
  • Save file as a.ssdl
  • 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';";

音盲 2024-12-21 20:29:02

这是我对类似场景的看法(与 @Greatran 答案非常相似,但我在 .EDMX 文件上找不到任何元数据工件处理属性项):

  1. 至少构建一次包含 EDMX 的项目文件
  2. 获取生成的 .SSDL 文件的副本,该文件包含在
    Your\Project\Folder\obj\Debug\edmxResourcesToEmbed\Your\ED​​MX\Namespace\

    的子路径

  3. 中并将其包含在您的项目下,例如与 EDMX 文件位于同一目录中: ...\Your\ED​​MX\Namespace\MyModel.ssdl
  4. 重命名它,添加后缀以匹配您的部署环境,例如调试环境将是MyModel.Debug.ssdl
  5. 从其文件属性中,将构建操作设置为嵌入资源
  6. 相关上下文连接的副本来自 Web 项目 Web.config 文件的字符串。这应该类似于
connectionString="metadata=res://\*/XXXXX.MyModel.csdl|
res://*/XXXXX.MyModel.ssdl|
res://*/XXXXX.MyModel.msl; 
provider=ZZZZZ;provider connection string='AAAAA'" 
  1. 为您的部署环境转换 Web 项目配置文件中的连接字符串(例如 Web.Debug.config),以便用您的自定义 SSDL 文件替换 EDMX 生成的 SSDL 文件:
connectionString="metadata=res://\*/XXXXX.MyModel.csdl|
res://*/YYYYY.MyModel.Debug.ssdl|
res://*/XXXXX.MyModel.msl;
provider=ZZZZZ;provider connection string='BBBBB'" 

最后一点可能会变得棘手,至少根据我的经验,因为 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):

  1. Build at least once your project containing the EDMX file
  2. Grab a copy of generated .SSDL file, contained in a subpath of
    Your\Project\Folder\obj\Debug\edmxResourcesToEmbed\Your\EDMX\Namespace\
  3. Copy and include that under your project, e.g. in the same directory as EDMX file: ...\Your\EDMX\Namespace\MyModel.ssdl
  4. Rename it adding a suffix to match your deploy environment, so e.g. for Debug environment it would be MyModel.Debug.ssdl
  5. From its file properties, set Build Action to Embedded Resource
  6. Grab a copy of related context connection string from the web project Web.config file. That should be something like
connectionString="metadata=res://\*/XXXXX.MyModel.csdl|
res://*/XXXXX.MyModel.ssdl|
res://*/XXXXX.MyModel.msl; 
provider=ZZZZZ;provider connection string='AAAAA'" 
  1. Transform that connection string in the web project config file for your deploy environment, say Web.Debug.config, in order to substitute the EDMX-generated SSDL file with your custom SSDL file:
connectionString="metadata=res://\*/XXXXX.MyModel.csdl|
res://*/YYYYY.MyModel.Debug.ssdl|
res://*/XXXXX.MyModel.msl;
provider=ZZZZZ;provider connection string='BBBBB'" 

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.

打小就很酷 2024-12-21 20:29:02

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.

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