8
我正在尝试使用(新)Fluent NHibernate(尝试从XML映射文件切换到FNH)。用下面的代码,我生成的数据库,我试图找到相同的解决方案,但与FNH(我想还是用hibernate.cfg.xml中):用Nhibernate使用流利NHibernate生成数据库
public void CreateDatabase()
{
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
cfg.Configure();
SchemaMetadataUpdater.QuoteTableAndColumns(cfg);
NHibernate.Tool.hbm2ddl.SchemaExport schema = new NHibernate.Tool.hbm2ddl.SchemaExport(cfg);
schema.Create(false, true);
}
namespace MyTestApplication.Entities
{
public class Person
{
public virtual int Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
}
}
namespace MyTestApplication.Data.Mapping
{
public class PersonMapping : ClassMap<Person>
{
public PersonMapping()
{
Id(x => x.Id);
Map(x => x.FirstName);
Map(x => x.LastName);
}
}
}
解决方案
最后,我用这个(感谢马尔科):
public void CreationDB()
{
FluentConfiguration config = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString("......"))
.Mappings(
m => m.FluentMappings.Add(typeof(MyTestApplication.Data.Mapping.PersonMapping)
));
config.ExposeConfiguration(
c => new SchemaExport(c).Execute(true, true, false))
.BuildConfiguration();
}
谢谢!你创造了我的一天,为我节省了数小时的研究 –