0

I develop an application using EF5 and I would like to know how to configure different connectionString (for example local and live). I know that all connectionsString are in app.config file:

<connectionStrings>
  <add name="MandatsEntitiesLocal" connectionString="metadata=res://*/MandatsModel.csdl|res://*/MandatsModel.ssdl|res://*/MandatsModel.msl;provider=System.Data.SqlClient;provider connection string='data source=&quot;localhost&quot;;initial catalog=&quot;UIVB Tests&quot;;user id=sa;password=***;MultipleActiveResultSets=True;App=EntityFramework'" providerName="System.Data.EntityClient" />
  <add name="MandatsEntities" connectionString="metadata=res://*/MandatsModel.csdl|res://*/MandatsModel.ssdl|res://*/MandatsModel.msl;provider=System.Data.SqlClient;provider connection string='data source=&quot;rmsi.net&quot;;initial catalog=&quot;UIVB Tests&quot;;user id=sa;password=***;MultipleActiveResultSets=True;App=EntityFramework'" providerName="System.Data.EntityClient" />
</connectionStrings>

I also know that I can switch between 2 different connection using DbContext constructor :

Partial Public Class MandatsEntities
Inherits DbContext

 Public Sub New()
    MyBase.New("name=MandatsEntities")
 End Sub

 Public Sub New(connectionName As String)
    MyBase.New(connectionName)
 End Sub

End Class

but I need to modify autogenerated class (and I think it's not recommanded)

So, to summarize if I have 10 contexts and 2 databases, I need to manage 20 connectionString !!! And I need to modify autogenerated class

Is there any method to do that properly? What is the best practice?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Cooxkie
  • 6,740
  • 6
  • 22
  • 26
  • 1
    Typically different environment connection strings are handled via web.config transforms. When you build/deploy for another environment it will automatically transform the configs for you. Do you need access to all environments while developing? – Dismissile Feb 28 '14 at 13:54
  • 1
    This is a standalone application, not web app ;-) – Cooxkie Feb 28 '14 at 13:57
  • @Cooxkie There is similar concepts for app.config using post / pre-build events – Simon Belanger Feb 28 '14 at 15:32

3 Answers3

0

Check out the Config Transform Nuget Package, it will allow you to perform Web.Config like transforms on a non-web project.

There is a walkthough of using this package in this another Stack Overflow question here.

Community
  • 1
  • 1
tom.dietrich
  • 8,219
  • 2
  • 39
  • 56
  • Thank you for your help, even if it wasn't what I was looking for, you help me in my research ;-) ! – Cooxkie Mar 11 '14 at 16:55
0

I don't know if this can directly help you, but I have had to handle different connection strings, for different entities before.

What I do is implement the logic in the repository.

If you want a context intialized with different connection strings, depending on the entity the repository has been initialized with, you can do something like:

//'Customers' and 'Orders' are in a different database than 'Products' and 'Vendors'...
private string[] _EntitiesGroup1 = { typeof(Customers).Name, typeof(Orders).Name };
private string[] _EntitiesGroup2 = { typeof(Products).Name, typeof(Vendors).Name };

In your repository constructor, you can do something like:

public Repository()
{
    if (Array.Exists(_EntitiesGroup1 , x => x == typeof(T).Name))
        _context = new Entities1();

    if (Array.Exists(_EntitiesGroup2 , x => x == typeof(T).Name))
        _context = new Entities2();


    _objectSet = _context.CreateObjectSet<T>();

}

Hope this somewhat helps.

Francis Ducharme
  • 4,848
  • 6
  • 43
  • 81
  • Thank you for your help, even if it wasn't what I was looking for, you help me in my research ;-) ! – Cooxkie Mar 11 '14 at 16:54
0

OK I found a solution and need to make some concession ;-) !

First I modified constructor of every Entities. I added new constructor with string parameter :

    'constructor auto-generated
    Public Sub New()
     MyBase.New("name=ViticulteurEntities")
    End Sub

    'constructor added by me
    Public Sub New(connectionName As String)
     MyBase.New(connectionName)
    End Sub

Second I created my own connection string :

viticulteurConnectionString = New System.Data.EntityClient.EntityConnectionStringBuilder() _
        With {
            .Metadata = "res://*/MandatsModel.csdl|res://*/MandatsModel.ssdl|res://*/MandatsModel.msl", _
            .Provider = "System.Data.SqlClient", _
            .ProviderConnectionString = "data source=rmsi.net;;initial catalog=&quot;UIVB Tests&quot;;user id=sa;password=***" _
             }.ConnectionString

Third, I used my own constructor with my own connection string ;-)

 Dim viticulteurContext As ViticulteurEntities
 .
 .
 .
 viticulteurContext = New ViticulteurEntities(viticulteurConnectionString)

sources :

Thanks all !

Community
  • 1
  • 1
Cooxkie
  • 6,740
  • 6
  • 22
  • 26