Pergunta

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 ?

Foi útil?

Solução

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.

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top