Fluent nhibernate 指定的表不存在
我试图使用流利的nhibernate,但我的nunit测试失败,出现了一个错误Fluent nhibernate 指定的表不存在,fluent-nhibernate,Fluent Nhibernate,我试图使用流利的nhibernate,但我的nunit测试失败,出现了一个错误 CookBook.Tests.FluentCategoryTests.CanCorrectlyMapCategory: System.Data.SqlServerCe.SqlCeException : The specified table does not exist. [ Category ] 这是我的分类对象 public class Category { public virtual int Id
CookBook.Tests.FluentCategoryTests.CanCorrectlyMapCategory:
System.Data.SqlServerCe.SqlCeException : The specified table does not exist. [ Category ]
这是我的分类对象
public class Category
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
这是测试
[TestFixture]
class FluentCategoryTests
{
private ISessionFactory _sessionFactory;
private RecipeConfiguration configuration = new RecipeConfiguration();
[TestFixtureSetUp]
public void TestSetup()
{
var cfg = new RecipeConfiguration();
_sessionFactory = Fluently.Configure()
.Database(MsSqlCeConfiguration.Standard.ShowSql().ConnectionString("Data Source=CookBook.sdf"))
.Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Category>(cfg)))
.BuildSessionFactory();
}
[Test]
public void CanCorrectlyMapCategory()
{
using (ISession session = _sessionFactory.OpenSession())
{
new PersistenceSpecification<Category>(session)
.CheckProperty(c => c.Id, 1)
.CheckProperty(c => c.Name, "Dessert")
.VerifyTheMappings();
}
}
}
[TestFixture]
类流分类测试
{
私人ISessionFactory(sessionFactory);;
私有RecipeConfiguration配置=新RecipeConfiguration();
[TestFixtureSetUp]
公共void TestSetup()
{
var cfg=新的RecipeConfiguration();
_sessionFactory=fluntly.Configure()
.Database(MsSqlCeConfiguration.Standard.ShowSql().ConnectionString(“数据源=CookBook.sdf”))
.Mappings(m=>m.AutoMappings.Add(AutoMap.AssemblyOf(cfg)))
.BuildSessionFactory();
}
[测试]
public void CanCorrectlyMapCategory()
{
使用(ISession session=\u sessionFactory.OpenSession())
{
新PersistenceSpecification(会话)
.CheckProperty(c=>c.Id,1)
.CheckProperty(c=>c.名称,“甜点”)
.验证应用程序();
}
}
}
那么我做错了什么呢?要回答您评论中的问题:
using NHibernate.Tool.hbm2ddl;
...
var cfg = new RecipeConfiguration();
_sessionFactory = Fluently.Configure()
.Database(MsSqlCeConfiguration.Standard.ShowSql().ConnectionString("Data Source=CookBook.sdf"))
.Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Category>(cfg)))
.ExposeConfiguration(config => new SchemaExport(config).Execute(false, true, false))
.BuildSessionFactory();
使用NHibernate.Tool.hbm2ddl;
...
var cfg=新的RecipeConfiguration();
_sessionFactory=fluntly.Configure()
.Database(MsSqlCeConfiguration.Standard.ShowSql().ConnectionString(“数据源=CookBook.sdf”))
.Mappings(m=>m.AutoMappings.Add(AutoMap.AssemblyOf(cfg)))
.ExposeConfiguration(config=>newschemaexport(config).Execute(false、true、false))
.BuildSessionFactory();
CookBook.sdf是否已经存在,或者是否为每个测试创建了它?如果创建了,您缺少新的SchemaExport(cfg).Execute(false,true,false),它在数据库中创建了架构谢谢,我回家后会检查一下。只是为了弄清楚它放在哪里:.Mappings(…).ExposeConfiguration(config=>SchemaExport(config).Execute(false,true,false))
谢谢。我想知道SchemaExport在哪个命名空间中?我得到的错误是找不到类型或命名空间名称“SchemaExport”(您是否缺少using指令或程序集引用?)