You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Entity Developer中配置NHibernate版本列为字节数组?

Mapping Byte[] Version Property to SQL Server Timestamp with Entity Developer & NHibernate

Absolutely, you can implement exactly the mapping you're targeting—NHibernate fully supports using a byte[] type for optimistic locking with SQL Server's timestamp (or rowversion) column. The catch is that Entity Developer's UI has a default restriction on visible Version property types, but we can work around that easily. Here's how:

Step 1: Set Up the Version Property in Entity Designer

First, create the Version property in your entity, but pick a temporary allowed type (like Int64) to satisfy the UI's initial validation. Make sure to mark it as a Version property:

  • Select the entity in the designer
  • Add a new property named Version
  • In the Properties panel, set Is Version to true
  • Temporarily set the Type to Int64 (since this is in the allowed list the UI shows)

Step 2: Customize the Mapping to Use Byte[] and Timestamp

You have two reliable ways to adjust the mapping so it uses byte[] and maps to SQL Server's timestamp type:

Option 1: Directly Configure in Entity Developer (Persistent Setting)

This is the best approach because it survives future code/mapping regenerations:

  1. Select your Version property again
  2. In the Properties panel, find the Type field and click the ellipsis (...) button to open the Type Selection dialog
  3. Switch to the Custom tab, then enter Byte[] in the Custom Type field
  4. Next, expand the Columns section for the property:
    • Set Sql Type to timestamp
    • Set Generated to Always
    • Set Nullable to false
  5. Save your model—Entity Developer will now generate the correct NHibernate mapping for you.

Option 2: Manually Edit the Generated XML Mapping File

If you just need a quick test or don't mind re-editing after regenerations, you can modify the XML directly:
Replace the auto-generated version element with your desired mapping:

<version name="Version" type="Byte[]" generated="always">
  <column name="Version" not-null="true" sql-type="timestamp" />
</version>

Step 3: Verify Entity Class Generation

Ensure your generated entity class has the byte[] type for the Version property:

  • If Entity Developer still generates the temporary Int64 type, adjust your code generation template:
    1. Open the Code Generation Options for your model
    2. Select the NHibernate entity class template
    3. Locate the section where Version properties are generated
    4. Modify the type output to use byte[] instead of the default type variable (e.g., replace <%= Property.Type %> with byte[] for this specific property)

Step 4: Test the Mapping

  • Generate your database schema script—confirm the Version column is created with the timestamp type
  • Run your application to verify optimistic locking works as expected: NHibernate will correctly read and update the timestamp column using the byte[] property value

A quick note: SQL Server's timestamp type is functionally identical to rowversion (the newer, preferred name), so you can use sql-type="rowversion" instead if you prefer—NHibernate will handle it the same way.

内容的提问来源于stack exchange,提问作者Michael Schulz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:02:45