Skip to content

3. Setting up a DB connection

Dmitry Savchenko edited this page Jul 15, 2016 · 5 revisions

To begin this process, create DB with which you will work. Open SQL Managment Studio and create two DB: Example and Example_test.

add_DB

example_db

example_test_db

In order to work with DB, you need to set up a connection. Add to the file Example.UI -> Web.config and Example.UnitTests -> app.config connection string to the BD:

Connection string
  <connectionStrings>
    <add name="Example" connectionString="Data Source=INCODING-PC\SQLEXPRESS;Database=Example;Integrated Security=false; User Id=sa;Password=1" providerName="System.Data.SqlClient" />
    <add name="Example_Test" connectionString="Data Source=INCODING-PC\SQLEXPRESS;Database=Example_Test;Integrated Security=true" providerName="System.Data.SqlClient" />
  </connectionStrings>
In the file Example.Domain -> Infrastructure -> Bootstrapper.cs, register the appropriate connection string using a key called 'Example':
Bootstrapper.cs: connection string
var configure = Fluently
        .Configure()
        .Database(MsSqlConfiguration.MsSql2008.ConnectionString(ConfigurationManager.ConnectionStrings["Example"].ConnectionString))
        .Mappings(configuration => configuration.FluentMappings.AddFromAssembly(typeof(Bootstrapper).Assembly))
        .ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true))
        .CurrentSessionContext();

In the file Example.UnitTests -> MSpecAssemblyContext.cs, register the connection string to the BD using the key called 'Example_test':

MSpecAssemblyContext.cs: connection string
var configure = Fluently
        .Configure()
        .Database(MsSqlConfiguration.MsSql2008
                                    .ConnectionString(ConfigurationManager.ConnectionStrings["Example_Test"].ConnectionString)
                                    .ShowSql())
        .Mappings(configuration => configuration.FluentMappings.AddFromAssembly(typeof(Bootstrapper).Assembly));