实体框架与ASP.NET MVC 2:即时选择数据库实例

发布于 2024-09-30 21:18:31 字数 692 浏览 1 评论 0原文

我目前正在开发一个 ASP.NET MVC 2 项目,该项目需要同一 SQL Server 2008 R2 数据库的多个实例(一个在生产中使用 SQL 复制,其他是它的开发对应项,允许架构更新并在本地测试我们的应用程序)生产版本,一个实例启用了 SQL 复制,另一个实例没有启用)。

目前,我们需要同一数据库的三个实例来完成我们的工作而不妨碍生产活动。

我不知道这是否是一个好方法,但我更改数据库实例的方法如下:

  1. 进入 web.config,删除活动数据库的连接字符串
  2. 删除当前的 Database.edmx
  3. 创建一个新的 EDMX指向另一个数据库
  4. 使用 XML 编辑器打开新的 Database.edmx 并将其替换为:
    <属性名称=“rowguid”类型=“uniqueidentifier”Nullable=“false”/>
    与此:
  5. 构建部署项目
  6. 在我们的生产服务器或测试服务器上安装安装程序server

有什么方法可以更快更方便地做到这一点吗?在项目中,或者在部署解决方案中?

I'm currently working on an ASP.NET MVC 2 project which needs several instances of the same SQL Server 2008 R2 database (One in production using SQL Replication, the others being it's development counterpart, allowing schema updates and testing of our applications locally before production release, one instance with SQL Replication on, another one without it).

Currently we need three instances of the same DB to do our jobs without impeding production activities.

I don't know if it's the good way around, but the way I change instance of the DB is the following :

  1. Go in the web.config, remove the connectionString of the active DB
  2. Delete the current Database.edmx
  3. Create a new EDMX to point to the other database
  4. Open the new Database.edmx with the XML Editor and replace this :
    <Property Name="rowguid" Type="uniqueidentifier" Nullable="false" />
    with this :
    <Property Name="rowguid" Type="uniqueidentifier" Nullable="false" StoreGeneratedPattern="Computed"/>
  5. Build the Deployment project
  6. Install the setup either on our production server, or on our test server

Is there any way to do this in a faster and more convenient way ? In the project, or in the deployment solution prehaps ?

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

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

发布评论

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

评论(2

不回头走下去 2024-10-07 21:18:31

是的,有一个更好的解决方案,那就是使用 EntityConnectionStringBuilder

您可以完全控制连接的所有方面,包括提供商、服务器/数据库、CSDL/SSDL/MSL、安全性等。

然后您可以动态分配所需的切换信息。

Yes, there is a better solution, and that is to use a EntityConnectionStringBuilder.

You have full control over all the aspects of the connection, including the provider, the server/database, the CSDL/SSDL/MSL, security, etc.

You could then dynamically assign the toggled information you require.

唯憾梦倾城 2024-10-07 21:18:31

这是部分解决方案...您可以覆盖 OnContextCreated 方法中的连接字符串。因此,您可以这样做:

Private Sub OnContextCreated()
            Dim fi As FieldInfo = GetType(ObjectContext).GetField("_connection", BindingFlags.Instance Or BindingFlags.NonPublic)
            Dim Environment As String = "" 'pull from web.config

            'Change the connection string based on which environment you are looking at
            If Environment = "dev" Then
                fi.SetValue(Me, New EntityConnection("dev connection string"))
            ElseIf Environment = "prod" Then
                fi.SetValue(Me, New EntityConnection("prod connection string"))
            End If

        End Sub

这样您就不必每次都删除并重新创建 .edmx 文件,因为连接字符串将指向正确的数据库 - 您唯一需要编辑的是 rowguid值,因为两个数据库之间的值不同。

需要考虑的另一件事...您可以将实体框架创建/更新/删除映射到自定义存储过程。因此,如果服务器之间只有一两列不同,则可以在任一服务器上拥有自定义存储过程来处理它们之间的列差异。

This is a partial solution...you can override the connection string in the OnContextCreated method. So, you could do something like this:

Private Sub OnContextCreated()
            Dim fi As FieldInfo = GetType(ObjectContext).GetField("_connection", BindingFlags.Instance Or BindingFlags.NonPublic)
            Dim Environment As String = "" 'pull from web.config

            'Change the connection string based on which environment you are looking at
            If Environment = "dev" Then
                fi.SetValue(Me, New EntityConnection("dev connection string"))
            ElseIf Environment = "prod" Then
                fi.SetValue(Me, New EntityConnection("prod connection string"))
            End If

        End Sub

This way you wouldn't have to delete and recreate the .edmx file every time, since the connection string would point to the correct database - the only thing you would have to edit would be the rowguid value, since that is different between the two databases.

One other thing to consider...you can map the entity framework create/update/delete to custom stored procedures. So if you only have a column or two that is different between the servers, you could have custom stored procs on either server that deal with the column differences between them.

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