WCF 数据服务更新实体错误和表主键类型
一段时间以来,当我尝试使用 WCF 数据服务和实体框架更新表中的字段时,我找不到错误的原因:
MydbEntities context = new MydbEntities(new Uri("http://localhost:53051/Services/MydbService.svc"));
MyEntity entitytoedit = context.MyEntity.FirstOrDefault();
entitytoedit.Name = "TheNewName";
context.UpdateObject(entitytoedit);
context.BeginSaveChanges(OnChangesSaved, context);
...
错误如下所示: RequestException 处理此错误时发生错误
修复方法是添加一个自动增量 decimal(18, 0)
字段作为具有身份规范 IsIdentity = yes
的主键,而不是只是一个varchar(20)
字段作为主键。
请有人解释一下问题的本质:我应该始终在 WCF 数据服务中使用自动增量主键吗?如果不是,我到底错在哪里?
For some time I couldn't find out the cause of an error when trying to update a field in a table using WCF Data Services plus Entity Framework:
MydbEntities context = new MydbEntities(new Uri("http://localhost:53051/Services/MydbService.svc"));
MyEntity entitytoedit = context.MyEntity.FirstOrDefault();
entitytoedit.Name = "TheNewName";
context.UpdateObject(entitytoedit);
context.BeginSaveChanges(OnChangesSaved, context);
...
The error was like following: RequestException An error occurred while processing this request.
The fix was to add an autoincremental decimal(18, 0)
field as the Primary Key with Identity Specification IsIdentity = yes
instead of just a varchar(20)
field as the Primary Key.
Please, could anybody explain the nature of the problem: should I always use autoincremental Primary Keys with WCF Data Services? If not, where in fact am I wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这更多是实体框架问题而不是 WCF 数据服务问题。
您可以手动设置主键字段,并且必须将
StoreGeneeratedPattern
设置为None
。或者您可以使用
AutoIncrement
字段作为主键,在这种情况下,您必须将其StoreGeneeratedPattern
设置为Identity
。将其混合到案例中总是会破坏事情。此外,当您执行 Model First 时,默认情况下主键字段的类型
int
和StoreGeneratePattern
设置为Identity
,因此只需将类型更改为 < code>decimal 就像你的情况是不够的。I think it is more Entity Framework question than WCF Data Services.
You can either have primary key field that you set by hands and you must set
StoreGeneratedPattern
toNone
.Or you can use
AutoIncrement
field as primary key and in this case you must setStoreGeneratedPattern
for it toIdentity
.Mixing this to cases always break the things. In addition when you do Model First, primary key field by default has type
int
andStoreGeneratedPattern
set toIdentity
, so just changing the type todecimal
as in your case is not enough.