0

I have a model that is created by EF6 ,by default my connection string is initialized in app.config as you can see here :

<connectionStrings>
    <add name="ShirazRailwayEntities" connectionString="metadata=res://*/RailWay.csdl|res://*/RailWay.ssdl|res://*/RailWay.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=****;initial catalog=DB-Metro;user id=sa;password=****;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

I have 3 layers in my application Model ,UI ,Domain class .my connection string is initialized in 'Model' and 'UI' layers,I need to set connection string by user , i mean the connection string should be set by user.

My question :

As i said i have 2 layers that the connection string are initialized inside them ,Is it necessary to initialized both connection by user ? Or just the UI is enough ?Which connection string should be initialized ?

The next question is how can i set the connection string by user?

I have a repository layer between my EF model and UI called repository :

   public class StationRepository : GenericRepository<ShirazRailWay.ShirazRailwayEntities, DomainClass.Station>
    {
    }

My Ui calls this repository .

best regards

Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180

2 Answers2

3

how can i set the connection string by user?

The DbContext class that underlies your Entity Framework context class has a constructor that takes in a connectionString parameter. If you expose that in your context, you can pass whatever connection string you want to it at runtime.

using(var ctx = new MyContext(GetCurrentUserConnectionString())
{
   ...
}

As for your other questions, I didn't really understand what you're asking. If you want a better answer, please clarify.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • My model and Ui are in separate project i should initialize the ui connection string too ? – Ehsan Akbar Jul 14 '14 at 15:56
  • I don't call my dbcontext directly i have an repository between my model and ui how can i pass the connection string to my model.i mean i call my dbcontext in repository but my connection string should be initialized in ui – Ehsan Akbar Jul 14 '14 at 15:58
  • @EA - And why is that a problem? – Erik Funkenbusch Jul 14 '14 at 16:03
  • @ErikFunkenbusch you know in my ui i have an app.config that my connection string is there ,in my repository layer i call my dbcontext ,i don't know how can i set connection string in ui by user and out it in dbcontext layer – Ehsan Akbar Jul 14 '14 at 16:05
  • 3
    @EA - The same way you do in your ui... I don't understand what problem you're having... it works exactly the same way. – Erik Funkenbusch Jul 14 '14 at 16:09
  • @ErikFunkenbusch coult you please take a look at this :http://stackoverflow.com/questions/24741575/passed-dbcontext-connection-string-to-dalayer – Ehsan Akbar Jul 14 '14 at 16:56
1

You can initialize your dbcontext with custom connection string:

var context = new DbContext(connectionString);

update:

In your repository you can add parameter to constructor of repository that will initialize connection string for dbcontext.

Example:

public class StationRepository : GenericRepository<ShirazRailWay.ShirazRailwayEntities, DomainClass.Station>
{
    public StationRepository(string connectionstring):base(connectionstring){}
}

public class GenericRepository<T1, T2>
{
    protected GenericRepository(string connectionstring)
    {
        //initialize dbcontext using connection string
    }
}
Kirill Bestemyanov
  • 11,946
  • 2
  • 24
  • 38