简体   繁体   English

流利的NHibernate:插入或更新时忽略映射属性“ Length”

[英]Fluent NHibernate: mapping property “Length” ignored when inserting or updating

we are trying to (slowly) migrate all our programs from Delphi to C#, and its our first time with an ORM, so please forgive if the question may be silly. 我们正在尝试(缓慢地)将所有程序从Delphi迁移到C#,这是我们第一次使用ORM进行迁移,因此,如果问题可能很愚蠢,请原谅。 ;-) ;-)

We are using: 我们正在使用:

  • DB2 Version 9.7.5 DB2版本9.7.5
  • FluentNHibernate Version 1.3.0.0 FluentNHibernate版本1.3.0.0
  • NHibernate Version 3.3.1.4000 NHibernate版本3.3.1.4000

For our unit test project, I got the entity "TestCSharp" defined. 对于我们的单元测试项目,我定义了实体“ TestCSharp”。 This type has a string property named "TextVar". 此类型具有一个名为“ TextVar”的字符串属性。 Here is the mapping: 这是映射:

public class TestCsharpMapping:ClassMap<TestCsharp>
{
  /// <summary> 
  /// map constructor
  /// </summary>
  public TestCsharpMapping()     
  {
     Schema("TEST");
     Table("TEST_CSHARP");
     base.Id(x => x.Id).Column("ID").GeneratedBy.Sequence("ID_SEQUENCE");
     Map(x => x.TextVar).Column("TEXT_VAR").Nullable().Length(20);
  }

}

The table TEST.TEST_CSHARP resides on a linux DB2 database, the field TEXT_VAR is a VARCHAR(20) field. 表TEST.TEST_CSHARP驻留在Linux DB2数据库上,字段TEXT_VAR是VARCHAR(20)字段。

This is my unit test (the hibernate session itself is encapsulated by a wrapper type we are designing, so dont you wonder): 这是我的单元测试(休眠会话本身由我们正在设计的包装器类型封装,所以您不要怀疑):

  [TestMethod]
  public void TestEditTextFields()
  {
     ReInitSession();
     CleanUp();
     var testobjects = _dataProviderSession.LoadAll<TestCsharp>(QueryDuration.ShortQuery);
     testobjects.Add( new TestCsharp());
     // overflow : Text_Var is only 20 chars long
     testobjects[0].TextVar = "01234567890123456789A";
     _dataProviderSession.Save();
     testobjects = _dataProviderSession.LoadAll<TestCsharp>(QueryDuration.ShortQuery);
     Assert.AreEqual(1, testobjects.Count);
     Assert.AreEqual("01234567890123456789", testobjects[0].TextVar);
  }

The unit test is failing at the Save() command with a DB2 error "IBM.Data.DB2.DB2Exception: ERROR [22001] [IBM] CLI0109E Zeichenfolge rechts abgeschnitten." 单元测试在Save()命令失败,并出现DB2错误“ IBM.Data.DB2.DB2Exception:错误[22001] [IBM] CLI0109E Zeichenfolge重新安装。” (which is german for "string right truncated"). (德语为“字符串右截断”)。 Assigning a string value with a length less or equal to 20 characters leads to no exception. 分配长度小于或等于20个字符的字符串值不会导致异常。

So what is it I am missing? 那我想念的是什么? The Length property doesn´t seem to have any functional impact. Length属性似乎没有任何功能上的影响。 I was assuming that the call "Length(20)" would cause the ORM to cut the string down to 20 characters when saving (just as Borlands Delphi did when setting the size of field components inside datasets). 我以为调用“ Length(20)”将导致ORM在保存时将字符串缩减为20个字符(就像Borlands Delphi在设置数据集中的字段分量的大小时所做的那样)。 :-/ : - /

Any hints are welcome! 任何提示都欢迎!

Kind regards 亲切的问候

Andreas 安德烈亚斯

Your assumption is incorrect. 您的假设是不正确的。 NHibernate will not truncate the string for you, which does not seem like a good idea anyway, as you are silently losing data. NHibernate不会为您截断该字符串,无论如何这都不是一个好主意,因为您正在悄悄地丢失数据。

Setting Length in the mapping will just set the database field length. 在映射中设置“长度”将仅设置数据库字段的长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM