NHibernate 无法附加 MDF
NHibernate Cannot attach the MDF
我正在尝试使用 NHibernate 附加到 SQL Server Express LocalDB,但我一直收到异常:
An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.
内部异常是:
Cannot attach the file 'Test.mdf' as database 'Test'.
我以前在使用 Entity Framework 时遇到过这个错误,可以通过以下方式解决:
> sqllocaldb stop v11.0
> sqllocaldb delete v11.0
然而,这在这种情况下不起作用。下面是我的完整程序:
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Mapping;
using NHibernate.Tool.hbm2ddl;
namespace Sandbox
{
public class Person
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Id(p => p.Id);
Map(p => p.Name);
}
}
public class Program
{
public static void Main(string[] args)
{
var connectionString =
@"Data Source=(LocalDB)\v11.0;AttachDBFilename=Test.mdf;" +
@"Initial Catalog=Test;Integrated Security=True";
var _sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2012.ConnectionString(connectionString))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>())
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(false, true))
.BuildSessionFactory();
}
}
}
问题是,与 Entity Framework 不同,NHibernate 不会为您创建数据库。我必须首先执行以下操作来创建数据库:
var databaseName = "Test";
var databaseFile = "Test.mdf";
if(!File.Exists(databaseFile))
{
var connectionString = @"Data Source=(LocalDB)\v11.0;Integrated Security=True;";
var query = $@"
CREATE DATABASE {databaseName} ON PRIMARY (
NAME = '{databaseName}',
FILENAME = '{databaseFile}');";
using(var connection = new SqlConnection(connectionString))
{
using(var command = new SqlCommand(query, connection))
{
connection.Open();
command.ExecuteNonQuery();
}
}
Console.WriteLine("Created database!");
}
注意:这不是生产质量代码,只是问题的演示。
我正在尝试使用 NHibernate 附加到 SQL Server Express LocalDB,但我一直收到异常:
An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.
内部异常是:
Cannot attach the file 'Test.mdf' as database 'Test'.
我以前在使用 Entity Framework 时遇到过这个错误,可以通过以下方式解决:
> sqllocaldb stop v11.0
> sqllocaldb delete v11.0
然而,这在这种情况下不起作用。下面是我的完整程序:
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Mapping;
using NHibernate.Tool.hbm2ddl;
namespace Sandbox
{
public class Person
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Id(p => p.Id);
Map(p => p.Name);
}
}
public class Program
{
public static void Main(string[] args)
{
var connectionString =
@"Data Source=(LocalDB)\v11.0;AttachDBFilename=Test.mdf;" +
@"Initial Catalog=Test;Integrated Security=True";
var _sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2012.ConnectionString(connectionString))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>())
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(false, true))
.BuildSessionFactory();
}
}
}
问题是,与 Entity Framework 不同,NHibernate 不会为您创建数据库。我必须首先执行以下操作来创建数据库:
var databaseName = "Test";
var databaseFile = "Test.mdf";
if(!File.Exists(databaseFile))
{
var connectionString = @"Data Source=(LocalDB)\v11.0;Integrated Security=True;";
var query = $@"
CREATE DATABASE {databaseName} ON PRIMARY (
NAME = '{databaseName}',
FILENAME = '{databaseFile}');";
using(var connection = new SqlConnection(connectionString))
{
using(var command = new SqlCommand(query, connection))
{
connection.Open();
command.ExecuteNonQuery();
}
}
Console.WriteLine("Created database!");
}
注意:这不是生产质量代码,只是问题的演示。