1

I am experiencing a mental block in searching/finding an example to this scenario.

User-A logs on and connects to "master" database on "abcServer" to verify account. Once approved, server returns connection string for "unique-A" database to user-A on "abcServer".

I know the dbContext in EF has the constructor assigning a connString name, but I want this to be dynamic:

(1) DbContext

    // dbContext constructor
    public MasterDatabase() : base("name=MasterDatabase")
    {
        Configuration.LazyLoadingEnabled = false;
    }

(2) The default connection is

  // web.config
  <connectionStrings>
    <add name="MasterDatabase" connectionString="Data Source=abcServer;Initial      
        Catalog=Master;Integrated Security=True;" providerName="System.Data.SqlClient" />
  </connectionStrings>

But, that is it for the hard coding of connStr. The remainder of the project is about the respective user working with the EF classes in his/her db which won't be determined until return connection string from a successful login. Something like this:

(3) Method in controller changing the conn string

   // in api controller, once log in successful, call to method return value as parameter
   private void Connect2DbwithThisUsersConnString(string connStr)
    {
   // change the conn string & USE THIS CONTEXT to this user's db
    }

Any asp.net/mvc examples of this? Thanks in Advance. Going nuts here.

Remember: I do not know the connection string until log in. This may be a new user, requiring a new database be created on the fly and that connStr passed along. I cannot hard-code a unique dbContext "base" or conn string.

Many thanks for your attention!

4

3 回答 3

1

您正在寻找具有每用户分片方法的实体框架多租户。

您需要两个完全独立的 EF 上下文。一,在 conf 文件中使用连接字符串,连接到登录数据库。此上下文仅包含与登录相关的对象/表。验证登录后,从登录数据库(EF 上下文)中检索租户特定位置,并使用包含每个租户对象/表的不同 EF 上下文来操作实际数据。使用适当构造的连接字符串打开此上下文。您仍然需要解决许多问题,主要是关于如何解决架构更改和数据库升级(迁移)等重要问题。

您还应该阅读有关基于 SQL Azure 联合的不同方法的实体框架和联合。

于 2013-08-06T15:21:21.113 回答
0

It is very difficult (if not impossible) to change a DbContext connection string after instantiating it.

You could make your UniqueConnString function return a new DbContext and create a new constructor in your MasterDatabase class accepting the connection string using this constructor in DbContext

private MasterDatabase GetUniqueConnString(string connStr)
{
    return new MasterDatabase(connStr);
}


public MasterDatabase() : this("name=MasterDatabase")
{
}
public MasterDatabase(string nameOrConnectionString) : base(nameOrConnectionString)
{
    Configuration.LazyLoadingEnabled = false;
}
于 2013-08-06T14:53:18.650 回答
0

这对我有用,有人看到错误或知道更好的方法吗?

// in controller/server //
private static void Init(string connStr)
 var connString = "Data Source=abcServer;Initial Catalog=" + dbName + ";Integrated Security=True;";
 var connBuilder = new SqlConnectionStringBuilder(connString);
 var defaultContext = new DefaultDb(); //Context #1 - default connection from web.config
 var otherContext = new OtherDb(connBuilder.ToString()); //Context #2 dynamic connection
 ..... 
 }

// in OtherContext.cs //
     public partial class OtherContext
     {
         public OtherContext(string connectionString) : base(connectionString) { }
     }

     public partial class OtherContext: DbContext   
     {
         public OtherContext() : base("name=DefaultConnection") { }
     }

    // the migration file //
     public class Configuration : DbMigrationsConfiguration<OtherContext>
     {
         public Configuration()
         {
             AutomaticMigrationsEnabled = true;
             AutomaticMigrationDataLossAllowed = true;
         }
     }
于 2013-08-06T17:09:24.903 回答