2017-02-18 19 views
0

私は "CassandraCSharpDriver"を使用していますが、同じアプリケーション内で異なるキースペースを動的に使用することはできません。クラス "テーブル"は、常にデータベースへの接続に使用した最初のキースペースに接続されています。ここでは、コードの例です:動的キースペースを切り替えることができません

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インスタンスです。

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"); 
:LINQのコンポーネントは the specific constructorを使用してモデルごとに異なるkeyspacesにマッピング複数のテーブルをサポートし、前記

関連する問題