Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am intermediate developer and I have a situation whereas a I have single WCF service hosted on a server that hosts a connection to a database. This is WCF service is currently accessed by a WinForms application. My customer would like to have the capability to be able to change the database being accessed when the WinForms application starts. On the database server there would be multiple separate databases that have identical structures and programability. Upon startup of the WinForms application, which connects to the one WCF service, the user would choose the database they wish to connect to. Not needing the capability of connecting to multiple databases simultaneously, just the ability to change which database to use at application startup.

This WCF use Entity Framework and I need to instantiate the context using the connection string that was sent to WCF.


What are the best way to implement this type of scenario?

Thank you.

What I have tried:

WCF:
amespace MSITrackerWS.Apps.Portal
{
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

public partial class PortalDB : DbContext
{

public PortalDB()
: base("name=MyConnectionString") //MyConnectionString = entry in web.config, I want to send connection string instead of having this here
{
}

public virtual DbSet<clientconfiguration> ClientConfigurations { get; set; }

}
}
Posted
Updated 10-Jun-16 3:20am
v3

1 solution

When you create your context object you can use the constructor overload that accepts the connect name or string as an argument;

DbContext Constructor (String) (System.Data.Entity)[^]

var ctx = new PortalDB("connection here");
 
Share this answer
 
Comments
Eikistein 10-Jun-16 10:39am    
Thank you, that help me. To have I complete solution, how can I create a constructor for the SVC that accepts a connections string?

PortalSVC:
Code suggested by you + constructor with connectionString as parameter

client:
PortalSVC portal = PortalSVC("datasource=...")


is that possible?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900