RavenDB-索引复制

本文关键字:复制 索引 RavenDB- | 更新日期: 2023-09-27 18:25:02

我正在尝试让索引复制工作,但似乎无法让它将数据复制到我的SQL数据库中。我已经看了这里的Docs和其他Stackoverflow项目,但我一定遗漏了什么。有人能给我指正确的方向吗?我想我已经接近了。

在我的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文件,但仍然存在问题。

RavenDB-索引复制

Scarpaci,您可能没有将连接字符串添加到服务器app.config。