WCF 数据服务保存数据/实体问题
我正在尝试通过 WCF 将数据保存回数据库,但我不断收到:
Cannot insert the value NULL into columns 'Id', table 'ResellerPlatform.dbo.Contacts';列不允许为空。插入失败。该语句已终止。
(System.Data.UpdateException)
通过服务端本身的 WebGet 也会发生同样的情况:
[WebGet]
public void AddContact()
{
var Contact = new Models.ResellerPlatform.Contact
{
Id = Guid.NewGuid(),
Name = "Foo"
};
base.CurrentDataSource.AddToContacts(Contact);
base.CurrentDataSource.SaveChanges();
}
Contact 模型是一个具有 Id(Guid)和 Name(字符串)的基本实体,没有关系或者任何东西。这是我的访问规则:
Config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
Config.SetEntitySetAccessRule("*", EntitySetRights.All);
我不明白为什么它不断抛出异常,因为 Id 不 null。想法、建议?
I'm trying to save data back to the database via WCF but I keep getting:
Cannot insert the value NULL into column 'Id', table 'ResellerPlatform.dbo.Contacts'; column does not allow nulls. INSERT fails. The statement has been terminated.
(System.Data.UpdateException)
The same happens via a WebGet on the service side itself:
[WebGet]
public void AddContact()
{
var Contact = new Models.ResellerPlatform.Contact
{
Id = Guid.NewGuid(),
Name = "Foo"
};
base.CurrentDataSource.AddToContacts(Contact);
base.CurrentDataSource.SaveChanges();
}
The Contact model is a basic entity with an Id (Guid) and a Name (string), no relationships or anything. Here's my access rules:
Config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
Config.SetEntitySetAccessRule("*", EntitySetRights.All);
I can't understand why it keeps throwing the exception as Id isn't null. Ideas, suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 Guid 作为主键时,将 StoreGeneratePattern 设置为 none 似乎可以解决问题 - 我认为 EF 可以将 Guid 处理为主键?
It seemed setting the StoreGeneratedPattern to none resolved the issue when using a Guid for the primary key - I thought EF could handle Guid's as primary keys?
在我看来,EF 期望数据库生成您的 Id 键列的值。
通常,您必须选择:
StoreGeneratePattern
属性设置为none
。StoreGeneratePattern
属性设置为Identity
。EF 版本 1 不支持第二个选项。您必须在 xml 编辑器中打开并修改 edmx 文件才能使其正常工作(请参阅此 博客条目了解详细信息)。
您所做的是两种选项的混合:您在客户端代码中设置值,但使用
StoreGeneratePattern=Identity
告诉 EF 数据库将生成该值。It sound to me like EF is expecting the database to generate the value of your Id key column.
Generally you have to options:
StoreGeneratedPattern
property tonone
.StoreGeneratedPattern
property toIdentity
.The second option is not supported with EF Version 1. You have to open and modify the edmx file in a xml editor to get this to work (see this blog entry for details).
What you did is a mix of both options: You set the value in your client code but tell EF with
StoreGeneratedPattern=Identity
that the database will generate the value.