I trying to use FluentNHibernate in my test project. The folowing code generate tables in existin database:
var sessionFactory = FluentNHibernate
.Cfg.Fluently.Configure()
.Database(
MsSqlConfiguration.MsSql2008.ConnectionString(
c => c.FromConnectionStringWithKey("DefaultConnection")))
.CurrentSessionContext("web")
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<SqlCommandFactory>())
.ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true))
.BuildSessionFactory();
It's works when database already exists. But how create database if it is absent? The connection string to database looks following:
Data Source=MAIN\SQLEXPRESS;Initial Catalog=Magazine;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False
Also i'm try using another method. Shown below.
new SchemaExport(cfg).Execute(true, true, false)
In this case i get another exception.
System.Data.SqlClient.SqlException: Cannot open database "Magazine" requested by the login. The login failed.
I change connection string.
Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\Magazine.mdf;Initial Catalog=Magazine;Integrated Security=True
LocalDb would be the best option in my case. But as in the first case with SQLEXPRESS I again received an exception.
System.Data.SqlClient.SqlException: Cannot attach the file '.....\App_Data\Magazine.mdf' as database 'Magazine'.
I'm used Code First for EF before and EF create database if it is missing. How can i create database using FluentNHibernate?