LINQ to SQL附加实体问题 [英] LINQ to SQL attach entity issue
问题描述
我正在尝试在通用存储库中创建更新方法作为数据访问层
我有一个这样的实体:
I''am trying to create an update method in a generic repository as a data access layer
i have an entity like this:
[Table]
public class Product
{
[Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "Int NOT NULL IDENTITY")]
public int Id { get; private set; }
[Column(UpdateCheck = UpdateCheck.Never)]
public string Name { get; set; }
....
}
我为id以外的所有字段都设置了"Update Check = true",并在attach方法中将"asModified"属性设置为true,结果如下:
i set `Update Check = true` for all the fields exept for the id and i set the `asModified` propery in the attach method to true which i found as following:
public void Update(T entity)
{
_db.GetTable<t>().Attach(entity, true);
_db.SubmitChanges();
}
</t>
但我不断遇到同样的异常:
如果实体声明了版本成员或没有更新检查策略,则只能将其以修改后的形式附加而没有原始状态.
那是什么问题呢?
或除了建立时间戳列作为版本号之外,是否建议其他方法在通用存储库中创建更新方法.
在此先感谢;
but i keep getting the same exception:
An entity can only be attached as modified without original state if it declares a version member or does not have an update check policy.
so what''s the problem ???
or Do you recommend any other approches to create an update method in a generic repository except createing a timestamp column as a version number.
Thanks in Advance;
推荐答案
在该表中包含一个时间戳列.会做到的.我的意思是时间戳数据类型.您可以随心所欲地调用它,例如. ts.
另外,我建议您查看LINQ的断开实体更改跟踪: http://linq2sqleb.codeplex.com/ [ ^ ]
Thx
include a timestamp column in that table. that will do it. I mean the timestamp data type. u can call it whatever u want, for eg. ts.
Else i sugesst you take a look at Disconnected Entity Change Tracking for LINQ: http://linq2sqleb.codeplex.com/[^]
Thx
这篇关于LINQ to SQL附加实体问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!