我是 Fluent 和 Nhibernate 的新手,所以也许有一个我在这里没有看到的简单问题。以下是我的映射
public class DriverStatusChangeMapping : ClassMap<DriverStatusChange>
{
public DriverStatusChangeMapping()
{
Id(x => x.Id).GeneratedBy.GuidComb();
Map(x => x.CreateReceivedAt);
Map(x => x.RemoveReceivedAt);
Map(x => x.Status);
Component<AuditAction>(x => x.Created, m =>
{
m.Map(x => x.ActionByUser).Column("CreatedByUser");
m.Map(x => x.ActionByUserType).Column("CreatedByUserType");
m.Map(x => x.ActionOccurredAt).Column("CreatedAt");
});
Component<AuditAction>(x => x.Removed, m =>
{
m.Map(x => x.ActionByUser).Column("RemovedByUser");
m.Map(x => x.ActionByUserType).Column("RemovedByUserType");
m.Map(x => x.ActionOccurredAt).Column("RemovedAt");
});
References(c => c.DriverLogEntry)
.Column("DriverLogEntryID")
.Access.Property()
.Cascade.All();
Table("tblDriverStatusChange");
}
}
我正在使用内存测试来验证存储库返回正确的数据。我的 RespositoryTestBase 如下。
public class RepositoryTestBase : IDisposable
{
private static NHConfiguration.Configuration _configuration;
private static ISessionFactory _sessionFactory;
protected ISession Session { get; set; }
public RepositoryTestBase()
{
_sessionFactory = CreateSessionFactory();
Session = _sessionFactory.OpenSession();
BuildSchema(Session);
}
private static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory().ShowSql())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Driver>())
.ExposeConfiguration(cfg => _configuration = cfg)
.BuildSessionFactory();
}
public void BuildSchema(ISession session)
{
var export = new SchemaExport(_configuration);
export.Execute(true, true, false, Session.Connection, null);
}
public void Dispose()
{
Session.Dispose();
}
}
现在,当我在会话中执行 get 时......我有 DriverStatusChange 记录
_session.Get<DriverStatusChange>(new Guid("6c9b5d4d-986d-4ff7-86ae-a05300d34a8b"));
但是如下的简单查询总是返回一个空列表。
_session.Query<DriverStatusChange>().Select(d => d).ToList();
以下是我使用 RepositoryTestBase 的测试类
public class DriverStatusChangeRepositoryTests:RepositoryTestBase
{
private IDriverStatusChangeRepository _driverStatusChangeRepository;
[SetUp]
public void Init()
{
var mockLogger = new Mock<ILogger>();
_driverStatusChangeRepository = new DriverStatusChangeRepository(Session, mockLogger.Object);
}
[TearDown]
public void TearDown()
{
Session.Clear();
}
[TestFixtureTearDown]
public void TestFixtureTearDown()
{
Dispose();
}
[Test]
public void get_where_removed_at_is_null()
{
var driverStatusChanges = CreateDriverStatusChanges(4);
var removedDriverStatusChange = driverStatusChanges[1];
removedDriverStatusChange.Removed = new AuditAction()
{
ActionByUser = "John Doe",
ActionByUserType = "User Type",
ActionOccurredAt = DateTime.UtcNow
};
removedDriverStatusChange.RemoveReceivedAt = DateTime.UtcNow;
_driverStatusChangeRepository.Update(removedDriverStatusChange);
var actual = _driverStatusChangeRepository.GetCurrent(10001, DateTime.MinValue, DateTime.UtcNow).Count;
Assert.AreEqual(3,actual, "Get current did not match");
}
private List<DriverStatusChange> CreateDriverStatusChanges(int number)
{
var driverStatusChanges = new List<DriverStatusChange>();
for (int i = 0; i < number; i++)
{
var driverStatusChange = new DriverStatusChange()
{
Created = GetCreated(i),
CreateReceivedAt = DateTime.UtcNow,
DriverLogEntry = new DriverLogEntry() {CreateReceivedAt = DateTime.UtcNow, Id = Guid.NewGuid(), Driver = new Driver() { DriverID = i+10000}},
Removed = null,
RemoveReceivedAt = null
};
_driverStatusChangeRepository.Add(driverStatusChange);
driverStatusChanges.Add(driverStatusChange);
}
return driverStatusChanges;
}
private AuditAction GetCreated(int number)
{
return
new AuditAction()
{
ActionByUser = "Jane Doe" + number.ToString(CultureInfo.InvariantCulture),
ActionByUserType = "User Type",
ActionOccurredAt = DateTime.UtcNow
};
}
}