0

我是 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
                };

    }




}
4

2 回答 2

1

尝试

_session.Query<DriverStatusChange>().ToList();

_session.Query 返回一个 IQueryable,您可以直接在其上调用“ToList()”。

编辑:(请忽略我上面的愚蠢)。

是因为您的会话在您获得记录之前就关闭并重新打开了吗?我很确定如果会话关闭,SQLite InMemory 就会下降。它将在下一次读取时重新创建,并且此时将为空。

一种解决方案是使用数据库连接提供程序来防止连接被关闭,这也将防止 InMemory 数据库丢失:

public class DatabaseConnectionProviderThatCachesTheConnection : DriverConnectionProvider
{
    static IDbConnection _databaseConnection;

    public override void CloseConnection(IDbConnection conn)
    {
        // Deliberately override to do nothing.
    }

    public override IDbConnection GetConnection()
    {
        if (_databaseConnection == null)
        {
            _databaseConnection = base.GetConnection();
        }

        return _databaseConnection;
    }
}

然后你可以在你的会话工厂中使用它:

 private static ISessionFactory CreateSessionFactory()
{
    return Fluently.Configure()
      .Database(SQLiteConfiguration
          .Standard
          .InMemory()
          .Raw(Environment.ConnectionProvider, 
               typeof(DatabaseConnectionProviderThatCachesTheConnection)
               .AssemblyQualifiedName)
          .ShowSql())
      .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Driver>())
      .ExposeConfiguration(cfg => _configuration = cfg)
      .BuildSessionFactory();
}
于 2012-05-16T18:17:26.387 回答
1

我没有看到任何交易或Session.Flush,所以我相信这就是您没有返回任何结果的原因,因为您的更改未提交。在CreateDriverStatusChanges, 之后

_driverStatusChangeRepository.Add(driverStatusChange);

添加

Session.Flush()

于 2012-05-17T19:07:26.987 回答