如何在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:
- Select your
Versionproperty again - In the Properties panel, find the Type field and click the ellipsis (
...) button to open the Type Selection dialog - Switch to the Custom tab, then enter
Byte[]in the Custom Type field - Next, expand the Columns section for the property:
- Set Sql Type to
timestamp - Set Generated to
Always - Set Nullable to
false
- Set Sql Type to
- 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
Int64type, adjust your code generation template:- Open the Code Generation Options for your model
- Select the NHibernate entity class template
- Locate the section where Version properties are generated
- Modify the type output to use
byte[]instead of the default type variable (e.g., replace<%= Property.Type %>withbyte[]for this specific property)
Step 4: Test the Mapping
- Generate your database schema script—confirm the
Versioncolumn is created with thetimestamptype - Run your application to verify optimistic locking works as expected: NHibernate will correctly read and update the
timestampcolumn using thebyte[]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

