流利的Nhibernate System.ApplicationException:对于属性"Id",预期类型为"System.Int32"的"1&q

编程入门 行业动态 更新时间:2024-10-27 20:23:52
本文介绍了流利的Nhibernate System.ApplicationException:对于属性"Id",预期类型为"System.Int32"的"1",但得到类型为"System.Int32"的"2"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好,我正在为流利的Nhibernate编写单元测试,当我在isloation中运行测试时,它通过了,但是当我运行多个测试时,则通过了.或多次运行测试失败,并显示以下消息 System.ApplicationException:对于属性"Id",预期类型为"System.Int32"的"1",但得到类型为"System.Int32"的"2"

Hi I am writing unit tests for fluent Nhibernate, when I run the test in isloation it passes, but when I run multiple tests. or run the test more than once it starts failing with the message below System.ApplicationException : For property 'Id' expected '1' of type 'System.Int32' but got '2' of type 'System.Int32'

[TextFixture] 公共无效Can_Correctly_Map_Entity() {

[TextFixture] public void Can_Correctly_Map_Entity() {

new PersistenceSpecification<UserProfile>(Session) .CheckProperty(c => c.Id, 1) .CheckProperty(c => c.UserName, "user") .CheckProperty(c => c.Address1, "Address1") .CheckProperty(c => c.Address2, "Address2")

}

推荐答案

我建议使用内存数据库测试映射,以便您可以仅将这些测试隔离在映射中.如果使用内存数据库,则可以将FluentConfiguration放在[TestInitialize](MSTest)或[SetUp](NUnit)方法中,并且每次都会从头开始在内存中创建数据库.这是一个示例:

I'd suggest testing your mappings using an in-memory database so that you can isolate these tests the mappings only. If you use an in-memory database, you can place the FluentConfiguration in the [TestInitialize] (MSTest) or [SetUp] (NUnit) method, and the db will be created from scratch (in memory) each time. Here's an example:

[TestInitialize] public void PersistenceSpecificationTest() { var cfg = Fluently.Configure() .Database(SQLiteConfiguration.Standard.InMemory().UseReflectionOptimizer()) .Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserProfile>()) .BuildConfiguration(); _session = cfg.BuildSessionFactory().OpenSession(); new SchemaExport(cfg).Execute(false, true, false, _session.Connection, null); }

然后您的测试应该在每次运行时都能正常运行

Then your test should work fine each time you run:

[TestMethod] public void CanMapUserProfile() { new PersistenceSpecification<UserProfile>(_session) .CheckProperty(c => c.Id, 1) .CheckProperty(c => c.UserName, "user") .CheckProperty(c => c.Address1, "Address1") .CheckProperty(c => c.Address2, "Address2") }

在这种情况下,您需要使用SQLite以及System.Data.SQLite DLL,可以在这里找到: sqlite.phxsoftware/

You'll need to use SQLite in this scenario, along with the System.Data.SQLite DLL, which you can find here: sqlite.phxsoftware/

希望有帮助.

更多推荐

流利的Nhibernate System.ApplicationException:对于属性"Id",预期类型为"System.I

本文发布于:2023-05-31 12:02:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/391387.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   流利   属性   System   Nhibernate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!