开发者

WCF Data Services saving data/entity issue

开发者 https://www.devze.com 2023-04-05 14:56 出处:网络
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

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开发者_C百科 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?


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?


It sound to me like EF is expecting the database to generate the value of your Id key column.

Generally you have to options:

  1. Generate the id value on the client side (c# code) as you did and set the StoreGeneratedPattern property to none.
  2. Generate the id value on the database side and set the StoreGeneratedPattern property to Identity.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消