c# - How to have application use different databases via wcf dataservice -
we building c# .net application sold clients, sql server database hosted us. have database each client. not sure if can use 1 wcf data service access different databases. using entity framework build database.
how can accomplish client can pass in correct database name or connection string?
this different databases using wcf dataservice said possible, doesn't specifics.
this wcf service multiple clients database(each client) looks same question, has no answers.
make wcf-service session based, provide login-method , in method have decide database use, can either change connectionstring edmx if datamodel same or if have differnt datamodels each client have create edmx instance each client!
here simple pseude-code, entityid identifies client
for creating entityconnectionstring check out link
to create session-based wcf service have define service interface that
[servicecontract(sessionmode = sessionmode.required)] public interface isampleservice { [operationcontract] void login(string user, string password, int entityid); }
and serviceimplementation should have these attributes, change these values based on needs
[servicebehavior(instancecontextmode = instancecontextmode.persession, concurrencymode = concurrencymode.single, automaticsessionshutdown = true)] public class sampleservice : isampleservice { sampleentities datacontext = null; public void login(string user, string password, int entityid) { if(checklogindata(user, password)) { initdatacontext(entity_id); } } private void initdatacontext(int entityid) { var connectionstring = getconnectionstringfromentityid(entityid); datacontext = new sampleentities(connectionstring); } private string getconnectionstringfromentityid(int entityid) { var providername = "system.data.sqlclient"; var servername = "localhost"; var databasename = getdatabasenamefromentityid(entityid); var sqlbuilder = new sqlconnectionstringbuilder(); sqlbuilder.datasource = servername; sqlbuilder.initialcatalog = databasename; sqlbuilder.integratedsecurity = true; var providerstring = sqlbuilder.tostring(); var entitybuilder = new entityconnectionstringbuilder(); entitybuilder.provider = providername; entitybuilder.providerconnectionstring = providerstring; entitybuilder.metadata = @"res://*/sampledatabase.csdl| res://*/sampledatabase.ssdl| res://*/sampledatabase.msl"; return entitybuilder.tostring(); } }
Comments
Post a Comment