EF 4.0保存未更改的相关记录,但仅保存这些记录中的时间戳列。 [英] EF 4.0 Saves unchanged related records, but only the timestamp columns in those records.
问题描述
我正在使用EF4.0。
我有一些带有时间戳列的表,I / EF用于并发检查。 它被配置为ConcurrencyMode = Fixed,StorGeneratedPattern = Computed,Type = binary,MaxLength = 8. 我将此列称为RowVersionNumber。
Table1。
- Table1ID = Identity INT
- RowVersionNumber = TimeStamp
Table2。
- Table2ID = Identity INT
- Table1 =外键到Table1ID
- Name = nvarchar(100)
我在表1中设置了一堆数据。 这些数据保持良好且永不改变。
几天后(即新的上下文)我创建了一个新的Table2。对象并将其与现有的Table1相关联。对象。 然后保存我的更改。
在EF3.5和EF4.0中,"在模型中包含外键列"未经检查,一切都按预期工作。
但是,如果我在EF4.0中使用"在模型中包含外键列"来构建模型检查后,我无法保存新的Table2。对象因为某些原因,EF 将对象插入到Table2中,并尝试更新
现有Table1对象上的timestamp列。
它在以下表格。
< font size ="2"color ="#0000ff">
UPDATE
table1
SET RowVersionNumber = xxx < font size ="2">
WHERE table1id = zzz
和 RowVersionNumber
= yyy
应该不要更改Table1,因此不需要更新时间戳。实际上,当多个用户创建与同一Table1对象相关的新table2对象时,这会导致concurreny错误。
什么有在EF4.0中出错?
我想在模型中选中"包括外键列",因为它有批量插入100 000或新对象时的一些性能优势。
我勾选"在模型中包含外键列"
Hello Kendall,
欢迎来到EF论坛!
它是EF4的已知问题。
相关实体’当我们使用导航属性设置关系时,将更新时间戳列。
解决方法是使用Foreign Key列直接设置关系。
例如
如果我们想要设置与现有Table1实体相关的Table2实体,我们可以使用以下代码:
=================== ================================================== =====
Table2 t2 = new Table2(){Name =“ Test”,
Table1 = t1.Table1ID };
context.Table2.Add(t2);
context.SaveChanges();
===================== ================ =====================================
它对你有用吗? span style ="">
美好的一天!
< span style ="font-size:12pt">
最诚挚的问候,b
Lingzhi Sun
MSDN订阅者支持
在论坛
如果您对我们的支持有任何反馈,请联系
msdnmg@microsoft.com
I am using EF4.0.
I have some tables with a timestamp column which I/EF uses for concurrency checking. It is configured as ConcurrencyMode=Fixed, StorGeneratedPattern=Computed, Type=binary, MaxLength=8. I call this column RowVersionNumber.
Table1.
- Table1ID = Identity INT
- RowVersionNumber=TimeStamp
Table2.
- Table2ID=Identity INT
- Table1=Foreign key To Table1ID
- Name = nvarchar(100)
I setup a bunch of data in Table 1. This data is persisted fine and never changes.
Some days later (i.e. new context) I create a new Table2. object and relate it to an existing Table1. object. Then save my changes.
In EF3.5 and EF4.0, with "Include Foreign key columns in the model" unchecked, everything works as expected.
However, If I build a model in EF4.0 with "Include Foreign key columns in the model" checked, I cannot save the new Table2. object as, for some reason EF inserts the object into Table2 as well as tries to update the timestamp column on the existing Table1 object.
It generates SQL in the following form.
UPDATE
table1
SET RowVersionNumber=xxx
WHERE table1id=zzz and RowVersionNumber = yyy
There should be no change to Table1, so no need to update the time stamp. Infact this results in concurreny errors when multiple users create new table2 objects relating to the same Table1 object.
What has gone wrong in EF4.0?
I would like to leave "Include Foreign key columns in the model" checked as this has some performance benefits when insert 100 000's or new objects in a batch.
I i tick the "Include Foreign key columns in the model"
Hello Kendall,
Welcome to EF forum!
It’s a known issue of EF4. The related entities’ timestamp columns will be updated when we use the navigation properties to set the relationship. The workaround would be using Foreign Key column to set the relationship directly.
E.g. if we want to set a Table2 entity related an existing Table1 entity, we can use the following codes:
==========================================================================
Table2 t2 = new Table2() { Name = “Test”, Table1 = t1.Table1ID };
context.Table2.Add(t2);
context.SaveChanges();
==========================================================================
Does it work for you?
Good day!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
这篇关于EF 4.0保存未更改的相关记录,但仅保存这些记录中的时间戳列。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!