如何有通过WCF DataService的应用程序使用不同的数据库 [英] How to have application use different databases via wcf dataservice

查看:152
本文介绍了如何有通过WCF DataService的应用程序使用不同的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在建设,将出售给客户一个C#.NET应用程序,SQL Server数据库将会由我们主办。我们将具有为每个客户端的数据库。我们不知道,如果我们可以用一个WCF数据服务来访问不同的数据库。使用实体框架构建数据库。

We are building a c# .Net application that will be sold to clients, the SQL Server database will be hosted by us. We will have a database for each client. We are not sure if we can use one WCF data service to access the different databases. Using Entity Framework to build the database.

我怎么能做到这一点,所以客户端可以通过正确的数据库名称或连接字符串中?

How can I accomplish this so the client can pass in the correct database name or connection string?

这<一href="http://stackoverflow.com/questions/3008949/different-databases-using-wcf-dataservice">Different使用WCF DataService的数据库,称这是可能的,但并没有真正进入细节。

This Different databases using WCF dataservice said it is possible, but doesn't really get into specifics.

这<一href="http://stackoverflow.com/questions/13757987/wcf-service-for-multiple-clients-with-databaseeach-for-client">WCF服务多个客户端与数据库(每个客户端)看起来是同样的问题,但一直没有答案。

This WCF Service for Multiple clients with database(each for client) looks to be the same question, but has no answers.

推荐答案

请您WCF的服务会话为主,提供一个登录法,在这种方法,你必须决定使用哪个数据库,你可以改变的ConnectionString为在EDMX 如果数据模型是相同的,或者如果你有不同的充DataModels为每一个客户,你必须为每个客户建立一个EDMX实例!

Make your WCF-Service Session based, provide a Login-Method and in this Method you have to decide which Database to use, you can either change the ConnectionString for the edmx if the DataModel is the same or if you have differnt DataModels for each client you have to create an edmx instance for each client!

下面一些简单的pseude- code,entityID标识Client

Here some simple pseude-code, entityID identifies the Client

有关创建EntityConnectionString看看这个<一href="http://msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnectionstringbuilder.aspx"相对=nofollow>链接

for creating an EntityConnectionString check out this Link

要创建一个基于会话的WCF服务,你必须定义这样的服务接口

To create a Session-Based WCF Service you have to define your Service Interface like that

[ServiceContract(SessionMode = SessionMode.Required)]
public interface ISampleService
{
    [OperationContract]
    void Login(string user, string password, int entityID);
}

和ServiceImplementation应该有这些属性,根据您的需求改变这些值

and the ServiceImplementation should have these Attributes, change these values based on your 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();
    }
}

这篇关于如何有通过WCF DataService的应用程序使用不同的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆