在实体框架的运行期间更改数据库,而不更改连接 [英] Change Database during runtime in Entity Framework, without changing the Connection

查看:188
本文介绍了在实体框架的运行期间更改数据库,而不更改连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务器托管50个具有相同模式的数据库,我想在下一个版本中开始使用Entity Framework。



我不需要一个新的每个这些数据库的连接。一个连接的权限可以与所有的50个数据库进行通信,而对于数据管理和速度(这是一个WebAPI应用程序),我不想在每次与每个数据库通信时实例化一个新的EF上下文不必,除非当然,如果发生这种情况,每次一个请求来到服务器然后没有什么大不了的事情。



我真正需要的是能够更改USE [databasename]命令,我认为最终将从EF发送到服务器。



有没有办法在代码中完成这个?在调用SaveChanges()等之前,EF会在上下文中保留一个引用可以随时更改的数据库名称的读/写属性。



谢谢你!!!



bob

解决方案

你可以看看at:




  • EntityFramework上下文

  • 而在 SO问题 https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.changedatabase(v=vs.110).aspx?f=255&MSPPError=-2147217396&cs-save-lang= 1& cs-lang = csharp#code-snippet-1rel =nofollow noreferrer>本文描述如何
    更改现有连接上的数据库。



    • 如果需要任何其他帮助,请通知我。



      已编辑

      更新第2链接指向 SqlConnection.ChangeDatabase 方法。

      所以最终的代码将看起来类似于以下内容:

        MetadataWorkspace workspace = new MetadataWorkspace(
      new string [] {res:// * /},
      new Assembly [] {Assembly.GetExecutingAssembly()});

      using(SqlConnection sqlConnection = new SqlConnection(connectionString))
      using(EntityConnection entityConnection = new EntityConnection(workspace,sqlConnection))
      using(NorthwindEntities context = new NorthwindEntities(entityConnection) )
      {
      //在默认数据库上执行任何操作
      foreach(上下文中的var product)产品
      {
      Console.WriteLine(product.ProductName);
      }

      //切换数据库
      sqlConnection.ChangeDatabase(Northwind);
      Console.WriteLine(Database:{0},connection.Database);
      }


      I have a server that hosts 50 databases with identical schemas, and I want to start using Entity Framework in our next version.

      I don't need a new connection for each of those databases. The privileges of the one connection can talk to all of the 50 databases, and for data management and speed (this is a WebAPI application) I don't want to instantiate a new EF context every time I talk to each of the databases if I don't have to, unless of course if this occurs each time a request comes to the server then no big deal.

      All I really need is the ability to change the USE [databasename] command, which I assume eventually gets sent to the server from EF.

      Is there a way to accomplish this in code? Does EF maintain a read/write property in the Context that refers to the database name that could be changed on the fly before calling SaveChanges(), etc.??

      Thank you!!!

      bob

      解决方案

      You can take a look at:

      • SO question about passing existing SQL Connection to EntityFramework Context
      • and at this article describing how to change database on existing connection.

      Please let me know if any additional help is needed.

      Edited
      Updated 2nd link to point to SqlConnection.ChangeDatabase method.
      So eventually code would look similarly to the following:

      MetadataWorkspace workspace = new MetadataWorkspace(
        new string[] { "res://*/" }, 
        new Assembly[] { Assembly.GetExecutingAssembly() });
      
      using (SqlConnection sqlConnection = new SqlConnection(connectionString))
      using (EntityConnection entityConnection = new EntityConnection(workspace, sqlConnection))
      using (NorthwindEntities context = new NorthwindEntities(entityConnection))
      {
        // do whatever on default database
        foreach (var product in context.Products)
        {
          Console.WriteLine(product.ProductName);
        }
      
        // switch database
        sqlConnection.ChangeDatabase("Northwind");
        Console.WriteLine("Database: {0}", connection.Database);
      }
      

      这篇关于在实体框架的运行期间更改数据库,而不更改连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆