WCF数据服务:SaveChanges:处理此请求时发生错误

时间:2010-08-02 16:50:36

标签: c# wcf rest wcf-data-services

当我尝试将一些更改从我的WPF应用程序保存到WCF数据服务时,我收到'处理此请求时出错'异常。加载所有记录工作正常,但保存它们不起作用。

希望你能提供帮助。

public partial class MainWindow : Window
{
    private DBEntities _dbEntities;

    public MainWindow()
    {
        InitializeComponent();
        _dbEntities = new DBEntities(new Uri("http://localhost:49256/DataService.svc/"));
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        foreach (var user in _dbEntities.User)
        {
            treeView1.Items.Add(user.Name);
        }
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            _dbEntities.MergeOption = MergeOption.AppendOnly;
            User user = new User(){Age = 1, ID = Guid.NewGuid(), Name = "Test"};
            _dbEntities.AddToUser( user);
            _dbEntities.SaveChanges();
        } catch(Exception ex)
        {
            MessageBox.Show(ex.Message+ ex.InnerException.Message);
        }
    }
}

没有更多的例外细节。

设置UseVerboseErrors = true后,将显示以下异常消息:

无法更新EntitySet'User',因为它具有DefiningQuery,并且元素中不存在支持当前操作的元素。

2 个答案:

答案 0 :(得分:1)

如果您的基础“用户”表没有primary key identified,则会出现此错误。您的实体是如何设置的(EF,LINQ-to-SQL等)以及您的底层存储库(SQL等)是什么?我会相应地更新我的答案。希望这有帮助!

答案 1 :(得分:0)

[ServiceBehavior(IncludeExceptionDetailInFaults=true)]
public class WcfDataService1 : DataService<PhaetonServiceEntities>
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
        // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
        // Examples:
         config.SetEntitySetAccessRule("Cities", EntitySetRights.All);
        // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
    }
}