WCF 数据服务更新实体错误和表主键类型

发布于 2024-11-14 06:03:51 字数 630 浏览 3 评论 0原文

一段时间以来,当我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱娆 2024-11-21 06:03:51

我认为这更多是实体框架问题而不是 WCF 数据服务问题。

您可以手动设置主键字段,并且必须将 StoreGeneeratedPattern 设置为 None

或者您可以使用 AutoIncrement 字段作为主键,在这种情况下,您必须将其 StoreGeneeratedPattern 设置为 Identity

将其混合到案例中总是会破坏事情。此外,当您执行 Model First 时,默认情况下主键字段的类型 intStoreGeneratePattern 设置为 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 to None.

Or you can use AutoIncrement field as primary key and in this case you must set StoreGeneratedPattern for it to Identity.

Mixing this to cases always break the things. In addition when you do Model First, primary key field by default has type int and StoreGeneratedPattern set to Identity, so just changing the type to decimal as in your case is not enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文