c# - 提交操作验证失败

标签 c# silverlight entity-framework mvvm

我正在使用带有 MVVM 概念和 Entity Framework 的 silver light 应用程序,但在更新值时遇到了一些麻烦。让我详细说明我的问题。我有三个表说 A、B 和 C,其中 B 与 A 有外键关系,C 与 B 有外键关系。我可以毫无问题地保存这些表。我正在使用 View 绑定(bind)网格并能够检索值以进行编辑但无法更新对数据库的任何更改。 更新时收到此错误 **

Message: Unhandled Error in Silverlight Application Code: 4004
Category: ManagedRuntimeError Message: System.ServiceModel.DomainServices.Client.DomainOperationException: Submit operation failed validation. Please inspect Entity.ValidationErrors for each entity in EntitiesInError for more information. en System.ServiceModel.DomainServices.Client.OperationBase.Complete(Exception error) en System.ServiceModel.DomainServices.Client.SubmitOperation.Complete(OperationErrorStatus errorStatus) en System.ServiceModel.DomainServices.Client.DomainContext.<>c__DisplayClassb.b__3(Object )

**

这里是 View 模型类..

public void Save(object obj)
    {   
            _currentCustomer.ModifiedBy = App.CurrentUser;
            _currentCustomer.ModifiedDateTime = System.DateTime.Now;

            foreach (BizFramework.Web.Model.Address address in AddressCollection.ToList())
            {
                string address1 = Convert.ToString(address.Address1);
                if (address1 != null && address1.Trim()!="")
                {                        
                    CVEReference = (from addref in _currentCustomer.CustomerVendorEmployeeReferences 
                                  where addref.CustomerID == _currentCustomer.CustomerID 
                                  select addref).SingleOrDefault();

                    BizFramework.Web.Model.Address addressExists = (from rec in CVEReference.Addresses
                                                                    where rec.AddressTypeID == address.AddressTypeID
                                                                    select rec).SingleOrDefault();
                    if (addressExists != null)
                    {
                        address.ModifiedBy = App.CurrentUser;
                        address.ModifiedDateTime = System.DateTime.Now;
                    }
                    else
                    {
                        address.AddressGuid = System.Guid.NewGuid();
                        address.ApplicationOwner = App.CurrentUser;
                        address.CreatedBy = App.CurrentUser;
                        address.ModifiedBy = App.CurrentUser;
                        address.CreatedDateTime = System.DateTime.Now;
                        address.ModifiedDateTime = System.DateTime.Now;

                        CVEReference.Addresses.Add(address);
                    }
                    
                }
                else
                {
                    //_currentCustomer.Addresses.Remove(address);
                    AddressCollection.Remove(address);
                    //dcBusinessAccountingContext.Addresses.Remove(address);
                }
            }                         

        dcBusinessAccountingContext.SubmitChanges();
    }

//Setting Table A from the view like this

_currentCustomer = (from CustomerAddress in dcBusinessAccountingContext.Customers
                                    where CustomerAddress.CustomerID == AddrView.CustomerID
                                    select CustomerAddress).SingleOrDefault();

其中_currentcustomer是A的实体对象,CVEReference是B的实体对象,AddrView是Table View的entityset,addresscollection是C的集合。

这个错误的原因可能是什么?

最佳答案

错误表明这是验证问题。 将 dcBusinessAccountingContext.SubmitChanges(); 更改为

dcBusinessAccountingContext.SubmitChanges(SubmitCallback, null);

然后你可以做一些错误检查:

private void SubmitCallback(SubmitOperation operation)
{
        if (operation.HasError)
        {
           //check "operation.EntitiesInError" for more details.
        }
}

关于c# - 提交操作验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7931483/

相关文章:

c# - 向图标添加叠加层时出现透明度问题

silverlight - 在 XAML 代码中编写条件语句

c# - 怎么画十字?

silverlight - 如何在 silverlight 文本框上设置垂直内容对齐

mysql - 无法加载文件或程序集 'MySql.Data

c# - 创建模型时无法使用上下文

c# - 语法:在方法调用前使用圆括号 ()

c# - 无法将自定义 ActiveX 控件添加到 .NET Core WinForms 应用程序,但 .NET Framework 应用程序工作正常

c# - Crystal Reports c# VS 2012 - 无法将图片添加到我的报告,不显示?

linq - 如何过滤嵌套集合 Entity Framework 对象?