.net - 为什么 NHibernate 会自动截断而不是在保存时抛出异常?

标签 .net unit-testing nhibernate transactions nhibernate-mapping

我一直在学习 NHibernate 教程的夏季类(class),当我完成第 04 节:探索事务和并发时 - 就像在教程中一样 - 我想创建一个 SAVE 测试,该测试应该因无效对象而失败。

目的是测试能否正常回滚事务。

想法是创建一个无效对象,该对象的属性的字符数多于 Db 设置的所需数量。但是NHibernate所做的是它获取第一个所需数量的字符并剪切其余的字符并将其成功保存到Db中

这是我的测试,没有失败:

   [Test]
public void AddCountryThrowExceptionOnFail()
{
    // This returns a Country with country code having more than 50 chars 
    // Where its length is set to 3 chars only          
    Country invalidCountry = GetInvalidCountry();
    _dataProvider.AddCountry(invalidCountry);
}

这是 AddCountry 方法:

public int AddCountry(Country country)
{
using (ISession session = GetSession())
{
    using (ITransaction tx = session.BeginTransaction())
    {
        try
        {
            int pk_Country = (int)session.Save(country);
            session.Flush();

            tx.Commit();

            return pk_Country;
        }
        catch(Exception)
        {
            tx.Rollback();
            throw;
        }
    }
}
}

这就是为 CountryCode 属性设置映射文件的方式:

<property name="CountryCode" column="CountryCode" type="string" length="3" not-null="true"></property>

那么,问题是,为什么 NHibernate 会获取映射文件中设置的 n=length 的前 N ​​个字符?如何防止这种情况发生,以免保存失败?

谢谢, 布拉克·奥兹多安

最佳答案

自动截断的不是 NHiberante,而是您的数据库。您可以尝试不同的数据库,看看有些会截断,有些不会,有些是可配置的。当将太长的字符串发送到不截断的数据库时,Nhiberante 会引发异常。

关于.net - 为什么 NHibernate 会自动截断而不是在保存时抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3499289/

相关文章:

php - PHP 中的单元测试?

nhibernate - 我可以使用 HQL 预先加载属性吗?

.net - 从 NHibernate 生成数据库模式脚本

.net - NHibernate 中没有 : CaSTLe. 代理的持久性。<EntityName>代理和懒惰的 ="true"?

c# - 命名空间 Microsoft 中不存在 Visualstudio,缺少程序集引用

c# - 如何测试lambda表达式的查询结果是否为null?

c# - 机器启动时运行exe

ios - 使用核心数据进行 Swift 4 单元测试

c# - 如何使用子对象字段作为 NHibernate 中另一个子对象的复合键的一部分

C# 模拟 IHttpclient & CreateClient