show_sql
在控制台中显示SQL交互的输出,除了特定的实体类型TimeOff
之外,其他都很好。插件未显示在控制台中。
显示输出:
var employee = new Employee();
_session.Save(employee);
这不是:
var timeOff = new TimeOff
{
Employee = new Employee{Id=1}
};
_session.Save(timeOff);
不过,插入操作仍在进行中。
这是我流畅的nhibernate配置和NUnit启动和拆卸:
[TestFixture]
public class InsertTests : IDisposable
{
public InsertTests()
{
var cs = ConfigurationManager.ConnectionStrings[0].ConnectionString;
var config = MsSqlConfiguration.MsSql2008
.ConnectionString(cs)
.ShowSql()
.FormatSql();
var sessionFactory = Fluently.Configure()
.Database(config)
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf<EmployeeMap>())
.BuildSessionFactory();
_session = sessionFactory.OpenSession();
}
private readonly ISession _session;
private ITransaction _transaction;
[SetUp]
public virtual void SetUp()
{
_transaction = _session.BeginTransaction();
}
[Test]
public void no_sql_output()
{
var timeOff = new TimeOff
{
Employee = new Employee{Id=1}
};
_session.Save(timeOff);
}
[Test]
public void shows_sql_output()
{
var employee = new Employee();
_session.Save(employee);
}
[TearDown]
public virtual void TearDown()
{
//_transaction.Rollback();
_transaction.Commit();
}
}
这是我的相关地图:
public class TimeOffMap : ClassMap<TimeOff>
{
public TimeOffMap()
{
Table("timeoff");
Id(x => x.Id).Column("timeoff_Id").GeneratedBy.Guid();
References(x => x.Employee);
}
}
public class EmployeeMap : ClassMap<Employee>
{
public EmployeeMap()
{
Table("employees");
Id(x => x.Id).Column("employee_id").GeneratedBy.Native();
HasMany(x => x.TimeOff)
.Cascade.All()
.Inverse()
.KeyColumn("employee_id");
}
}
这两个映射之间的唯一区别是TimeOff有一个Guid Id,Employee有一个Int。如果我在同一事务中插入Employees和TimeOff,则只显示EmployeeInsert。
这里的区别在于TimeOff
实体的实际DB保存是延迟的,并且仅在事务提交(在您的情况下是测试拆卸TearDown
方法(或显式_session.Flush
调用时发生。所以我相信这就是为什么您在测试用例方法can_add_timeoff
输出中看不到它的原因。