gpt4 book ai didi

unit-testing - Nhibernate 和 SQLite 异常 "Cannot write to a Closed TextWriter"

转载 作者:行者123 更新时间:2023-12-03 19:07:11 24 4
gpt4 key购买 nike

我正在尝试使用 NHibernate 和 SQLite 进行单元测试。但我不断收到错误

ADOException was unhandled by user code While preparing INSERT INTO User (First_Name, Last_Name, UserName, Password, Email, Active, Default_Clinic_Identification_Number, Login_Icon, Created_Date, Created_By, Modified_Date, Modified_By) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11); select last_insert_rowid() an error occurred

Inner Exception: {"Cannot write to a closed TextWriter."}



我不知道我做错了什么。这是我的代码
public class InMemoryDatabaseTest : IDisposable
{
private static Configuration Configuration;
private static ISessionFactory SessionFactory;
protected ISession session;

public InMemoryDatabaseTest()
{
if (Configuration == null)
{
Assembly a = Assembly.Load("Astute.Framework.Data");

Configuration = new Configuration()
.SetProperty(Environment.ReleaseConnections, "on_close")
.SetProperty(Environment.Dialect, typeof(SQLiteDialect).AssemblyQualifiedName)
.SetProperty(Environment.ConnectionDriver, typeof(SQLite20Driver).AssemblyQualifiedName)
.SetProperty(Environment.ConnectionString, "data source=:memory:")
.SetProperty(Environment.ProxyFactoryFactoryClass, typeof(ProxyFactoryFactory).AssemblyQualifiedName)
.AddAssembly(a);

SessionFactory = Configuration.BuildSessionFactory();
}

session = SessionFactory.OpenSession();

new SchemaExport(Configuration).Execute(true, true, false, session.Connection, Console.Out);
}


public void Dispose()
{
session.Dispose();

}
}

[TestClass]
public class Test : InMemoryDatabaseTest
{
[TestMethod]
public void CanSaveUser()
{
object id;

using (var tx = session.BeginTransaction())
{
id = session.Save(new User
{
FirstName = "Imran",
LastName = "Ashraf",
UserName = "imran",
Password = "Test",
Email = "Test@test.com",
IsActive = true,
DefaultClinicIdentifcationNumber = "",
LoginIcon = "",
CreatedBy = 1000000,
CreatedDate = DateTime.Today,
ModifiedBy = 1000000,
ModifiedDate = DateTime.Today
});

tx.Commit();
}

session.Clear();

}
}

我在这条线上遇到错误 id = session.Save。我从 http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx 得到这个例子

任何的想法?
提前致谢。

最佳答案

当您尝试写入 Console.Out 时,会出现错误消息“Inner Exception: {"Cannot write to a closed TextWriter."}”。 .

我创建了一个新的 TextWriter并使用它:

TextWriter writer = null;
new SchemaExport(Configuration).Execute(true, true, false, session.Connection, writer);

这解决了该异常。

关于unit-testing - Nhibernate 和 SQLite 异常 "Cannot write to a Closed TextWriter",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4819091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com