简体   繁体   中英

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. 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:

Unable to update the EntitySet 'User' because it has a DefiningQuery and no element exists in the element to support the current operation.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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