并发时间戳记列保持为空且已计算 [英] Concurrency timestamp column stays null with computed
问题描述
我正在使用EF-Model First保存我的实体.我在"RowVersion"列中添加了以下选项:
I'm using EF - Model First for saving my entities. I've added a column "RowVersion" with the options:
在元数据类中,添加了注释[Timestamp].
In a metadata class I added the annotation [Timestamp].
但是当我在表中添加/插入实体时,属性RowVersion保持为空.现在插入将不会,因为我将列设置为可空,但否则它将不断遇到异常,告诉我RowVersion为NULL.
But when I add/insert an entity in the table the property RowVersion stays null. Now the insert won't because I set the column nullable but otherwise it keeps running into an exception telling me that RowVersion is NULL.
我该如何解决?
推荐答案
我看到的是完全相同的行为.我整个下午都在工作,能够上班的唯一一件事就是在此链接上实施解决方案: http://www.undisciplinedbytes.com/2012/03/创建带有实体框架的时间戳列/
I am seeing the exact same behavior. I have worked on this all afternoon and the only thing that I was able to get to work was to implement the solution at this link: http://www.undisciplinedbytes.com/2012/03/creating-a-timestamp-column-with-entity-framework/
以下是从链接复制的步骤:
Here are the steps copied from the link:
采用模型优先方法的时间戳列
现在,使用模型优先"方法设置此列有点棘手.EF模型本身还没有内置对此的支持,因此,我们必须破解代码生成模板才能满足我们的需求.
Now, setting this column with the model-first approach is a little bit trickier. There’s no built-in support for this in EF’s model itself (yet), so we’ll have to hack the code generation template to fulfill our needs.
我们需要使用模型优先方法设置时间戳列的内容如下:
What we need to do to set up a timestamp column using model first approach is the following:
- 在EF模型中为实体添加名为时间戳"的属性
- 将类型设置为二进制
- 将null设置为false
- 将StoreGeneratedPattern设置为已计算"
- 将ConcurrencyMode设置为Fixed
- 创建SSDLToSQL10.tt的副本(通常在C:\ Program Files(x86)\ Microsoft Visual Studio 10.0 \ Common7 \ IDE \ Extensions \ Microsoft \ Entity Framework Tools \ DBGen中找到)
- 编辑显示以下内容的行:
[<#= Id(prop.Name)#>]<#= prop.ToStoreType()#><#= WriteIdentity(prop,targetVersion)#><#= WriteNullable(prop.Nullable)#><#=(p< entitySet.ElementType.Properties.Count-1)吗?,":"#>
[<#=Id(prop.Name)#>] <#=prop.ToStoreType()#> <#=WriteIdentity(prop, targetVersion)#> <#=WriteNullable(prop.Nullable)#><#=(p < entitySet.ElementType.Properties.Count - 1) ? "," : ""#>
将其更改为:
[<#= Id(prop.Name)#>]< #if(string.Compare(prop.Name,"TimeStamp",true)== 0){#> TIMESTAMP<#}否则{#><#= prop.ToStoreType()#><#}#><#= WriteIdentity(prop,targetVersion)#><#= WriteNullable(prop.Nullable)#><#=(p<entitySet.ElementType.Properties.Count-1)?,":"#>
[<#=Id(prop.Name)#>] <#if (string.Compare(prop.Name,"TimeStamp",true) == 0) { #>TIMESTAMP<# } else { #><#=prop.ToStoreType()#><# } #> <#=WriteIdentity(prop, targetVersion)#> <#=WriteNullable(prop.Nullable)#><#=(p < entitySet.ElementType.Properties.Count - 1) ? "," : ""#>
这会将所有称为时间戳记"(不区分大小写)的列更改为时间戳记"列.
This will change any column that is called "Timestamp" (case insensitive) to be a Timestamp column.
- 点击实体画布,然后将DDL生成模板设置为该文件的新副本
- 单击从模型生成数据库"10.享受新的并发感知数据访问权限!
这篇关于并发时间戳记列保持为空且已计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!