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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

My model and Ui are in separate project i should initialize the ui connection string too ?
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
@EA - And why is that a problem?
@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
@EA - The same way you do in your ui... I don't understand what problem you're having... it works exactly the same way.
|
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
    }
}

7 Comments

yes you are right but my problem is i have 3 layers in my app ,one of them is My model that my entities are there and ui and domain class ,if i set the connection string in model i should initialize it in ui too
Does every user has his own connection string? And how do you authenticate this user in your program?
I just need the user be able to choose ip address of server
In your repository you can add parameter to constructor of repository that will initialize connection string for dbcontext.
@it is that i want ,so could you please tell m in details?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.