我正在使用带有 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/