2012-02-19 108 views
0

我想獲得索引複製的工作,我似乎無法得到它將數據複製到我的SQL數據庫。我已經看過Docs和另一個Stackoverflow項目here,但我必須缺少一些東西。任何人都可以將我指向正確的方向嗎?我覺得我很接近。RavenDB - 索引複製

索引「Questions/VoteTotals」確實存在於我的Raven實例中。

代碼:

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

     using (var documentStore = new DocumentStore { Url = "http://localhost:8080" }.Initialize()) 
     { 
      documentStore.DatabaseCommands.PutIndex("Questions/VoteTotals", 
                new IndexDefinitionBuilder<Question> 
                { 
                 Map = questions => from question in questions 
                      select new 
                      { 
                       question.Title, 
                       VoteCount = question.Votes.Count 
                      } 
                }, 
                overwrite: true); 

      using(var s = documentStore.OpenSession()) 
      { 
       var q = new Question 
       { 
        Id = "questions/6", 
        Title = "How to replicate to SQL Server!?", 
        Votes = new List<Vote> 
        { 
         new Vote {Up = true, Comment = "Good!"}, 
         new Vote {Up = false, Comment = "Nah!"}, 
         new Vote {Up = true, Comment = "Nice..."}, 
         new Vote {Up = false, Comment = "No!"}, 
        } 
       }; 



      var replicationDocument = new Raven.Bundles.IndexReplication.Data.IndexReplicationDestination 
       { 
        Id = "Questions/VoteTotal", 
        ColumnsMapping = 
         { 
          {"Title", "Title"}, 
          {"UpVotes", "UpVotes"}, 
          {"DownVotes", "DownVotes"}, 
         }, 
        ConnectionStringName = "Reports", 
        PrimaryKeyColumnName = "Id", 
        TableName = "QuestionSummaries" 
       }; 


       s.Store(q); 
      s.Store(replicationDocument); 
       s.SaveChanges(); 
      } 

     } 
    } 

    private static void CreateRdbmsSchema() 
    { 
     var connectionStringSettings = ConfigurationManager.ConnectionStrings["Reports"]; 
     var providerFactory = DbProviderFactories.GetFactory(connectionStringSettings.ProviderName); 
     using (var con = providerFactory.CreateConnection()) 
     { 
      con.ConnectionString = connectionStringSettings.ConnectionString; 
      con.Open(); 

      using (var dbCommand = con.CreateCommand()) 
      { 
       dbCommand.CommandText = @"IF OBJECT_ID('QuestionSummaries') is not null 
               DROP TABLE [dbo].[QuestionSummaries] 
             "; 
       dbCommand.ExecuteNonQuery(); 

       dbCommand.CommandText = @"CREATE TABLE [dbo].[QuestionSummaries] 
             (
               [Id] [nvarchar](50) NOT NULL, 
               [VoteCount] [int] NOT NULL, 
               [Title] [nvarchar](255) NOT NULL 
             ) 
             "; 
       dbCommand.ExecuteNonQuery(); 
      } 
     } 
    } 
} 


public class Question 
{ 
    public string Id { get; set; } 
    public string Title { get; set; } 

    public List<Vote> Votes { get; set; } 
} 

public class Vote 
{ 
    public bool Up { get; set; } 
    public string Comment { get; set; } 
} 

編輯

每Ayende的建議我加入

<connectionStrings> 
    <add name="Reports" providerName="System.Data.SqlClient" connectionString="Data Source=.\sqlexpress2008r2;Initial Catalog=Test;Integrated Security=True"/> 

到Raven.Server.exe.config文件,但仍然有的問題。

回答

1

Scarpacci, 您可能沒有將連接字符串添加到服務器 app.config。

+0

我會看看謝謝你的建議 – scarpacci 2012-02-20 14:21:28

+0

我將連接字符串添加到Raven.Server.exe.config文件並重新運行場景....但仍然沒有看。必須是我的用戶錯誤,只是不知道還有什麼要檢查。 – scarpacci 2012-02-20 14:33:17