【发布时间】:2016-04-10 07:31:41
【问题描述】:
我正在使用 Silverlight 4 和 DevForce 6.1.11.0
我有一些实现 EntityAspect 的 POCO 类。
我使用 WebClient 从不同的设备获取这些实体。这些设备没有 DevForce 服务器。
将实体添加到实体管理器时,我首先使用entityManager.FindEntities<PocoSomeEntity>(EntityState.AllButDetached) 检查缓存中是否不存在具有该键的实体。然后我创建实体并像这样添加它:
entityManager.AddEntity(entity);
entity.EntityAspect.AcceptChanges();
我可以添加、修改和删除实体并将它们保存回设备 - 到目前为止没有问题。
最近我使用entity.EntityAspect.RemoveFromManager(true); 实现了“清除缓存”
如果我删除一个实体 (EntityAspect.Delete()),然后将其从管理器中删除,然后尝试将其重新加载,这似乎可以正常工作。在重新加载的实体上调用 EntityAspect.AcceptChanges() 时,它会引发“已经存在”异常。
我该如何解决这个问题?
编辑
抛出异常的是AddEntity()。
这是堆栈跟踪:
at IdeaBlade.EntityModel.EntityGroup.AddToKeyMap(EntityAspect aspect)
at IdeaBlade.EntityModel.EntityGroup.AddEntityCore(EntityAspect aspect)
at IdeaBlade.EntityModel.EntityGroup.AddAttachedEntity(EntityAspect aspect, EntityState entityState)
at IdeaBlade.EntityModel.EntityManager.AttachEntityAspect(EntityAspect aspect, EntityState entityState)
at IdeaBlade.EntityModel.EntityManager.AttachEntity(Object entity, EntityState entityState)
at IdeaBlade.EntityModel.EntityManager.AddEntity(Object entity)
at ...
我的实体有一个复合键。 我搜索了缓存,但一无所获:
// returns nothing
var instancesInManager = entityManager.FindEntities<PocoSomeEntity>(EntityState.AllButDetached).Where(i => i.p_key1 == 41 && i.p_key2 == 5448);
// returns nothing
var detachedInstancesInManager = entityManager.FindEntities<PocoSomeEntity>(EntityState.Detached).Where(i => i.p_key1 == 41 && i.p_key2 == 5448);
我也在不使用密钥的情况下进行搜索,但找不到任何可以解释这种行为的东西:
// returns instances, but none have keys with zeros or the key that I am looking for.
var instancesInManager = entityManager.FindEntities<PocoSomeEntity>(EntityState.AllButDetached);
// returns no results
var detachedInstancesInManager = entityManager.FindEntities<PocoSomeEntity>(EntityState.Detached);
EDIT2
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using IdeaBlade.Core.DomainServices;
using IdeaBlade.EntityModel;
using IbVal = IdeaBlade.Validation;
namespace ServerModel
{
[DataContract(IsReference = true)]
public class PocoSomeEntity : IKnownType, IHasPocoEntityAspect, INotifyPropertyChanged
{
public PocoSomeEntity () { }
private int m_key1;
[Key]
public int p_key1
{
get { return m_key1; }
set { m_key1 = value; OnPropertyChanged("p_key1"); }
}
private int m_key2;
[Key]
public int p_key2
{
get { return m_key2; }
set { m_key2 = value; OnPropertyChanged("p_key2"); }
}
...
#region IHasPocoEntityAspect Members
[Display(AutoGenerateField = false)]
[IgnoreDataMember]
public IdeaBlade.EntityModel.EntityAspect EntityAspect
{
get;
set;
}
#endregion
#region INotifyPropertyChanged Members
/// <summary>
/// This interface implementation is needed if you want EntityManager to automatically listen
/// to any property change.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(String propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
var args = new PropertyChangedEventArgs(propertyName);
handler(this, args);
}
}
#endregion
}
}
【问题讨论】:
-
我在测试中没有看到这个问题。如果密钥已存在于 EM 缓存中,则在执行 AddEntity() 时将引发异常。在添加之前检查 EntityKey 的值 - 如果它是 0 或可能有助于缩小问题范围的临时值,或者为我们提供更多信息来诊断它。
-
我在帖子中添加了更多详细信息。在做 AddEntity 之前,该实体没有 EntityAspect,我无法检查 EntityKey。在 AddEntity 之后,它会引发异常。在此之后,EntityKey 是正确的,但实体是分离的。
-
错误信息中显示的实体键是否是正确的键?我仍然想知道它是否可能被临时 ID 生成器设置为零或分配负 ID。如果看起来符合预期,您还可以从正确的键值构造一个 EntityKey,然后调用 em.FindEntity(entityKey) 以查看是否在缓存中找到匹配项。
-
是的,错误信息中的key是正确的key。如果我使用正确的键 ({PocoSomeEntity:41,5448}) 调用 FindEntity(),它会返回一个带有键的分离实体:{PocoSomeEntity:0,0} ???
-
PocoSomeEntity 是什么样的?你能发布它的定义吗?