我正在尝试将NHibernate与SQLite一起用于单元测试。但我一直在收到错误
用户代码未处理ADOException 准备INSERT INTO用户(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);选择last_insert_rowid()发生错误
内部异常:{“无法写入已关闭的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
得到了这个例子有什么想法吗? 提前谢谢。
答案 0 :(得分:2)
错误消息“内部异常:{”无法写入已关闭的TextWriter。当您尝试写入Console.Out
时,会给出<} em。。
我创建了一个新的TextWriter
并使用了它:
TextWriter writer = null;
new SchemaExport(Configuration).Execute(true, true, false, session.Connection, writer);
修正了这个例外。
答案 1 :(得分:0)
我看不出问题。我用它作为连接字符串:
Data Source=:memory:;Version=3;New=True;
您可以尝试查看它是否解决了问题。 New=True;
部分看起来很有希望。
答案 2 :(得分:0)
就我而言,我注意到我使用了InMemoryDatabaseTest
的两个实例。我从测试中删除了一个实例。