I am trying to get the index replication to work and I can’t seem to be get it to replicate data to my SQL DB. I have looked at the Docs and the other Stackoverflow item here, but I must be missing something. Can anyone point me in the right direction? I think I am close.
Index “Questions/VoteTotals” does exist in my Raven instance.
Code:
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; }
}
EDIT
Per Ayende’s suggestion I added
<connectionStrings>
<add name="Reports" providerName="System.Data.SqlClient" connectionString="Data Source=.\sqlexpress2008r2;Initial Catalog=Test;Integrated Security=True"/>
To the Raven.Server.exe.config file, but still having issues.
Scarpacci,
You probably didn’t add the connection string to the server app.config.