EF6映射导致奇怪的更新查询 [英] EF6 mapping results in strange update query

查看:130
本文介绍了EF6映射导致奇怪的更新查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计,


我的ODATA服务中的
我使用EF6来处理SQL Server。

EDMX也是为了表部门。在该表中,存在用于并发目的的TimeStamp字段。是的,我在代码中添加了[Timestamp]数据注释。在EDMX中,字段对于并发模式是固定的。



当我在我的网站datagrid中选择一个部门并更新字段时,我收到消息:

无法更新时间戳列。





在SQlProfiler中,我看到更新传递:

< pre lang =SQL> exec sp_executesql N ' 更新[dbo]。[部门]
SET [ModificationDate] = @ 0,[ModifiedBy] = @ 1
WHERE(([Id] = @ 2)AND([ModificationDate] = @ 3))
'
,N ' @ 0 binary(8),@ 1 int,@ 2 int, @ 3 binary(8)',@ 0 = 0x0000000000000805,@ 1 = 27,@ 2 = 3,@ 3 = 0x0000000000000805
go





然后我注意到好奇的事情。它想要更新ModificationDate字段(Timestamp),而我想更新实体中的字段DepartmentName。



如何克服这种奇怪的行为?



UPDATE!

我已将列设置为已计算,现在该列未添加到查询的SET部分。

但是为什么EF的更新查询中没有设置字段DepartmentName?



我尝试了什么:



这是我的代码。部门名称已设定! (当执行RowUpdating命令时,SelectedEntity在aspx.cs代码中设置。

 如果( SelectedEntity ==  null 
SelectedEntity = new 部门();
else
SelectedEntity = MarinGlobalConnection.MarinGlobalDataService._marinGlobal.Departments.First(x = > x.Id == SelectedEntity.Id);
SelectedEntity.DepartmentName = afdelingNaam;
SelectedEntity.IsDeleted = isdeleted;
SelectedEntity.ModifiedBy = CurrentUser.Instance.CurrentConnected == null ?( int ?) null :CurrentUser.Instance.CurrentConnected.ID;
if (SelectedEntit y.Id == 0
MarinGlobalConnection.MarinGlobalDataService._marinGlobal.AddToDepartments(SelectedEntity);
else
{
if (CompareModificationdate(modificationdate))
MarinGlobalConnection.MarinGlobalDataService._marinGlobal.UpdateObject(SelectedEntity);
}
await SaveChangesAsync();

解决方案

您从未提供过在EF中编码的数据库架构的任何详细信息,因此这只是猜测。 br $> b $ b

您所看到的是预期的行为。



您不应该对EF中标记为[Timestamp]的列。时间戳字段完全由EF管理。它们用于管理并发性,绝不应成为数据模型的一部分。


Hi Folks,

in my ODATA service I use EF6 to work with SQL Server.
An EDMX is created also for the table Departments. In that table there is a TimeStamp field for Concurrency purposes. And yes I have added [Timestamp] data annotation in the code. And in the EDMX the field is FIXED for Concurrency Mode.

When I select a department in my website datagrid and update the field I get the message:

Cannot update a timestamp column.



In SQlProfiler I see the update passing:

exec sp_executesql N'UPDATE [dbo].[Department]
SET [ModificationDate] = @0, [ModifiedBy] = @1
WHERE (([Id] = @2) AND ([ModificationDate] = @3))
',N'@0 binary(8),@1 int,@2 int,@3 binary(8)',@0=0x0000000000000805,@1=27,@2=3,@3=0x0000000000000805
go



And then I notice the curious thing. It wants to update the ModificationDate field (Timestamp) while I want to update the field DepartmentName in the entity.

How can I overcome this strange behaviour?

UPDATE!
I have set the Column to Computed and now the column is not added to the SET part of the query.
But why the field DepartmentName is not being set in the update query by EF?

What I have tried:

This is my code. The Departmentname is set! (SelectedEntity is set in the aspx.cs code when the RowUpdating command is being executed.

if (SelectedEntity == null)
                   SelectedEntity = new Department();
               else
                   SelectedEntity = MarinGlobalConnection.MarinGlobalDataService._marinGlobal.Departments.First(x => x.Id == SelectedEntity.Id);
               SelectedEntity.DepartmentName = afdelingNaam;
               SelectedEntity.IsDeleted = isdeleted;
               SelectedEntity.ModifiedBy = CurrentUser.Instance.CurrentConnected == null ? (int?)null : CurrentUser.Instance.CurrentConnected.ID;
               if (SelectedEntity.Id == 0)
                   MarinGlobalConnection.MarinGlobalDataService._marinGlobal.AddToDepartments(SelectedEntity);
               else
               {
                   if (CompareModificationdate(modificationdate))
                       MarinGlobalConnection.MarinGlobalDataService._marinGlobal.UpdateObject(SelectedEntity);
               }
               await SaveChangesAsync();

解决方案

You never gave any details about your database schema as coded in EF, so this is just a guess.

What you're seeing is the expected behavior.

You are NOT supposed to make any changes to a column tagged [Timestamp] in EF. Timestamp fields are entirely managed by EF. They are used to manage concurrency and should never be a part of your data model.


这篇关于EF6映射导致奇怪的更新查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆