我正在使用 RIA 域服务,实体框架 4 和 silverlight 4。当我保存更改时,当服务调用返回时,会调用一些域服务函数,将值设置为“”,根本不应该更改。
我有两个实体
服务.元数据.cs:
public partial class EntityA
{
[Key]
public Guid EntityA_Id { get; set; }
public string Name { get; set; }
public int EntityB_Id { get; set; }
[Include]
public EntityB entityB { get; set; }
}
public partial class EntityB
{
[Required]
public string Name { get; set; }
public int EntityB_Id { get; set; }
public EntityCollection<EntityA> entityA { get; set; }
}
在客户端,我在 EntityA 上有一个 Extra 属性来公开 Name 属性 od EntityB。服务器端和域服务永远不需要知道这个属性,它只用于 GUI。
public partial class EntityA
{
//Tags I have tried:
//[IgnoreDataMember]
//[XmlIgnore]
//[Ignore]
//[Exclude]
public string NameOf_EntityB
{
get
{
return this.entityB == null ? string.Empty : this.entityB.Name;
}
set
{
this.entityB.Name = value;
}
}
}
如果我编辑 entityA 的名称并调用 serviceContext.SubmitChanges(),当调用返回时,某些域服务进程正在设置 EntityA.NameOf_EntityB = ""; 因此,从用户的角度来看,他们保存了一个值,而另一个空白。
我需要阻止这种情况发生。我尝试了各种数据属性,但它们要么在客户端不起作用,要么没有效果。
知道如何阻止域服务更改此值吗?
这是更改值之前的调用堆栈:
System.ServiceModel.DomainServices.Client!System.ServiceModel.DomainServices.Client.ObjectStateUtility.**ApplyValue**(object o, object value, System.Reflection.PropertyInfo propertyInfo, System.Collections.Generic.IDictionary<string,object> originalState, System.ServiceModel.DomainServices.Client.LoadBehavior loadBehavior) + 0x74 bytes
System.ServiceModel.DomainServices.Client!System.ServiceModel.DomainServices.Client.ObjectStateUtility.ApplyState(object o, System.Collections.Generic.IDictionary<string,object> stateToApply, System.Collections.Generic.IDictionary<string,object> originalState, System.ServiceModel.DomainServices.Client.LoadBehavior loadBehavior) + 0x330 bytes
System.ServiceModel.DomainServices.Client!System.ServiceModel.DomainServices.Client.Entity.ApplyState(System.Collections.Generic.IDictionary<string,object> entityStateToApply, System.ServiceModel.DomainServices.Client.LoadBehavior loadBehavior) + 0x68 bytes
System.ServiceModel.DomainServices.Client!System.ServiceModel.DomainServices.Client.Entity.Merge(System.ServiceModel.DomainServices.Client.Entity otherEntity, System.ServiceModel.DomainServices.Client.LoadBehavior loadBehavior) + 0x5a bytes
System.ServiceModel.DomainServices.Client!System.ServiceModel.DomainServices.Client.DomainContext.ApplyMemberSynchronizations(System.Collections.Generic.IEnumerable<System.ServiceModel.DomainServices.Client.ChangeSetEntry> changeSetResults) + 0x10e bytes
System.ServiceModel.DomainServices.Client!System.ServiceModel.DomainServices.Client.DomainContext.ProcessSubmitResults(System.ServiceModel.DomainServices.Client.EntityChangeSet changeSet, System.Collections.Generic.IEnumerable<System.ServiceModel.DomainServices.Client.ChangeSetEntry> changeSetResults) + 0x262 bytes
System.ServiceModel.DomainServices.Client!System.ServiceModel.DomainServices.Client.DomainContext.CompleteSubmitChanges(System.IAsyncResult asyncResult) + 0x1cb bytes
System.ServiceModel.DomainServices.Client!System.ServiceModel.DomainServices.Client.DomainContext.SubmitChanges.AnonymousMethod__5() + 0x2e bytes
编辑: 现在找到了解决方法。在 ServiceContext.submitChanges() 调用的回调中,我可以调用 ServiceContext.RejectChanges() 来撤消对 EntityB 所做的更改。我不相信这个解决方案,因为在异步调用返回之前可能已经进行了其他更改,并且这些更改也将被拒绝。理想的解决方案是忽略该值并且根本不设置