SQL Server中的ServiceStack ormlite RowVersion

时间:2014-08-21 13:58:48

标签: servicestack ormlite-servicestack

为什么ormlite需要命名我的版本行RowVersion

签入代码如下:

var isRowVersion = propertyInfo.Name == ModelDefinition.RowVersionName
                && propertyInfo.PropertyType == typeof(ulong);

和这个实现

var isRowVersion = propertyInfo.FirstAttribute<RowVersionAttribute>();

会更加优雅和灵活(RowVersionAttribute中已经存在ServiceStack.DataAnnotations)这将允许使用byte[]作为字段类型,并且可以更容易地从实体框架移动。< / p>

为什么ModelDefinition.RowVersionName不变,而不是静态属性或lambda,至少可以使用不同的名称。是否可以使用目前名称不同于RowVersion的列?

感谢

1 个答案:

答案 0 :(得分:1)

RowVersion实现为Id之类的特殊属性,其签名强制为ulong RowVersion,例如:

public class Poco
{
    ...
    public ulong RowVersion { get; set; }
}

这确保了签名和名称不应该消除歧义,并且每个表上应该只有只有1 这样的属性(例如Id)。但就像其他字段一样,您可以使用[Alias]将其映射到具有不同名称的预先存在的列。

使用ulong确保在所有主要的RDBMS中有效且一致地实现RowVersion,即:

  • 在SqlServer中使用rowversion数据类型
  • 使用PostgreSql的xmin系统列(不需要表格列)
  • 在MySql,Sqlite和Oracle上使用 UPDATE触发器,其生命周期附加到创建/删除表API