2017-02-18 62 views
0

我正在使用「CassandraCSharpDriver」,我不能在同一個應用程序中動態使用不同的密鑰空間。類「Table」總是連接到我用來連接數據庫的第一個鍵空間。下面是代碼的例子:無法切換動態密鑰空間

class Program 
    { 
     static void Main(string[] args) 
     { 
      { 

       IDseCluster cluster = DseCluster.Builder() 
           .AddContactPoint("myPoint") 
           .Build(); 

       IDseSession session3 = cluster.Connect("keyspace_1"); 
       Row row3 = session3.Execute("select * from user_by_id").First(); 
       Console.WriteLine("keyspace_1 without table " + row3.GetValue<string>("username")); 
       //Result keyspace_1 without table user_from_keyspace_1 

       IDseSession session2 = cluster.Connect("keyspace_2"); 
       Row row2 = session2.Execute("select * from user_by_id").First(); 
       Console.WriteLine("keyspace_2 without table " + row2.GetValue<string>("username")); 
       //Result keyspace_2 without table user_from_keyspace_2 



      } 
      { 

       IDseCluster cluster = DseCluster.Builder() 
           .AddContactPoint("myPoint") 
           .Build(); 
       IDseSession session2 = cluster.Connect("keyspace_1"); 
       var table2 = new Table<UserByIdModel>(session2); 
       var user2 = table2.Execute().ToList().First(); 
       Console.WriteLine("keyspace_1 using table " + user2.UserName); 
       //Result keyspace_1 using table user_from_keyspace_1 


       IDseSession session = cluster.Connect("keyspace_2"); 
       var table = new Table<UserByIdModel>(session); 
       var user = table.Execute().ToList().First(); 
       Console.WriteLine("keyspace_2 using table " + user.UserName); 
       //Result keyspace_2 using table user_from_keyspace_1 


      } 
     } 
    } 

請幫我=)

回答

1

理想情況下,你應該每介紹不同的表中的一個不同的類。 Linq component of the DataStax driver將使用創建Table<T>實例時定義的配置來確定它映射到哪個密鑰空間/表。當使用new Table<UserByIdModel>(ISession session)構造函數時,使用的映射配置是可重用的MappingConfiguration.Global實例。

這就是說,LINQ的部件使用the specific constructor支持每模型不同keyspaces映射多個表:

var config = MappingConfiguration.Global; 
const string table = "user_by_id"; 
var table1 = new Table<UserByIdModel>(session1, config, table, "keyspace_1"); 
var table2 = new Table<UserByIdModel>(session2, config, table, "keyspace_2");