简体   繁体   English

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

[英]WCF Data Services: SaveChanges: An error occurred while processing this request

I get an 'An error occurred while processing this request' Exception when I try to Save some changes from my WPF-Application to a WCF-Data-Service. 尝试将一些更改从WPF应用程序保存到WCF数据服务时,出现“处理此请求时发生错误”异常。 Loading all Records works fine, but saving them doesn't work. 加载所有记录工作正常,但保存它们无效。

Hope you can help. 希望能对您有所帮助。

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);
        }
    }
}

There are no more exception details. 没有更多的异常详细信息。

After setting UseVerboseErrors = true the following exception message appears: 设置UseVerboseErrors = true后,将出现以下异常消息:

Unable to update the EntitySet 'User' because it has a DefiningQuery and no element exists in the element to support the current operation. 无法更新EntitySet'User',因为它具有DefiningQuery并且该元素中不存在任何元素来支持当前操作。

You can get this error if your underlying 'User' table doesn't have a primary key identified . 如果基础“用户”表未标识主键,则会出现此错误。 How is your entity set up (EF, LINQ-to-SQL, etc.) and what's your underlying repository (SQL, etc.)? 如何设置您的实体(EF,LINQ-to-SQL等)以及基础存储库(SQL等)是什么? I'll update my answer accordingly. 我将相应地更新答案。 Hope this helps! 希望这可以帮助!

[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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM