首页 新闻 搜索 专区 学院

entityFrameworks 1:n 无法删除的问题,还请各位帮忙看下,谢谢了

0
悬赏园豆:50 [已解决问题] 解决于 2011-07-21 10:13

我有两个模型,为CE  和DeviceInterface ,其中,CE 模型具有属性ICollection<DeviceInterface> DeviceInterfaces

之后我使用的是repository模式,现想删除掉CE 底下的多个DeviceInterface,我是这样做的

DeviceInterfaceRepository.Delete(DeviceInterfaceRepository.GetById(Id)); 

出现如下错误

AnalysisSNMPTest.entityFrameworkTesting.TestDeleteSub:System.InvalidOperationException : Adding a relationship with an entity which is in the Deleted state is not allowed.

在此之间,我没有使用其他Repository,特没有让entityFramework 读出CE 的模型
我被折磨了好久, Google出的方式好像都不是太管用,

各位拜托了,邮箱jicheng1014#gmail.com  先谢谢了

问题补充:

辜负大家希望了,谢谢站长,谢谢今朝

首先是repository

public class DeviceInterfaceRepository : RepositoryBase<DeviceInterface>, IDeviceInterfaceRepository
{
public DeviceInterfaceRepository(IDatabaseFactory factory) : base(factory) { }

public override void Delete(DeviceInterface entity)
{
base.Delete(entity);


}
}

public interface IDeviceInterfaceRepository : IRepository<DeviceInterface>
{

}

Base 的代码

public abstract class RepositoryBase<T> where T : Entity
{
protected static AnalysisContext dataContext;
protected readonly IDbSet<T> dbset;
protected RepositoryBase(IDatabaseFactory databaseFactory)
{
DatabaseFactory
= databaseFactory;
dbset
= DataContext.Set<T>();
}

protected IDatabaseFactory DatabaseFactory
{
get;
private set;
}

protected AnalysisContext DataContext
{
get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
}
public virtual void Add(T entity)
{
dbset.Add(entity);
}
public virtual void Update(T entity)
{

dataContext.Entry(entity).State
= EntityState.Modified;
}
public virtual void Delete(T entity)
{
dbset.Remove(entity);
}
public virtual void Delete(Expression<Func<T, bool>> where)
{
IEnumerable
<T> objects = dbset.Where<T>(where).AsEnumerable();
foreach (T obj in objects)
dbset.Remove(obj);
}
public virtual T GetById(int id)
{
return dbset.SingleOrDefault(p => p.Id == id);
}
public virtual T GetById(string id)
{
return dbset.Find(id);
}
public virtual IEnumerable<T> GetAll()
{
return dbset.ToList();
}
public virtual IEnumerable<T> GetMany(Expression<Func<T, bool>> where)
{
return dbset.Where(where).ToList();
}
public T Get(Expression<Func<T, bool>> where)
{
return dbset.Where(where).FirstOrDefault<T>();
}

public virtual void Save()
{
dataContext.SaveChanges();
}
}

Factory的写法

public class DatabaseFactory : Disposable, IDatabaseFactory
{
private AnalysisContext dataContext;


public AnalysisContext Get()
{
return dataContext ?? new AnalysisContext();
}
protected override void DisposeCore()
{
if (dataContext != null)
dataContext.Dispose();
}
}

之后是模型,所有模型继承于Entity

public class Entity
{
/// <summary>
/// 主ID
/// </summary>
public int Id { get; set; }
/// <summary>
/// 辅助记录信息
/// </summary>
[DisplayName("备注")]
public string Tips { get; set; }
}

之后CE 的模型类似这种

public class CE : Entity
{

//其他字段

private ICollection<DeviceInterface> deviceInterfaces;
/// <summary>
/// CE下面的interface信息
/// </summary>
public virtual ICollection<DeviceInterface> DeviceInterfaces
{
get
{
if (deviceInterfaces == null)
{
deviceInterfaces
= new List<DeviceInterface>();
}
return deviceInterfaces;
}
set { deviceInterfaces = value; }
}
}

之后这是DeviceInterface的模型

public class DeviceInterface : Entity
{
//其他属性。。。。。。

public int? CeId { get; set; }


private CE _CE;

public virtual CE CE
{
get { return _CE; }
set { _CE = value; }
}

}

之后运行 CeService的如下方法出错

public class CEService : ICEService
{
private readonly ICERepository ceRepository;
private readonly ICEDistributeIpFieldRepository ceDistributeIpFieldRepository;
private readonly IDeviceInterfaceRepository DeviceInterfaceRepository;


public CEService(ICERepository ceRepository,
ICEDistributeIpFieldRepository ceDistributeIpFieldRepository,
IDeviceInterfaceRepository DeviceInterfaceRepository)
{
this.ceDistributeIpFieldRepository = ceDistributeIpFieldRepository;
this.ceRepository = ceRepository;
this.DeviceInterfaceRepository = DeviceInterfaceRepository;
}

// 。。。。。。其他

public void emptyInterfacesInfo(CE ce)
{
var delList
= ce.DeviceInterfaces.ToList();
delList.ForEach(p
=>
{
DeviceInterfaceRepository.Delete(DeviceInterfaceRepository.GetById(p.Id));
});
//此处报错
DeviceInterfaceRepository.Save();

}

注入为

public class IoC
{
static IUnityContainer container = new UnityContainer()

.RegisterType
<IDatabaseFactory, DatabaseFactory>()

.RegisterType
<ICERepository, CERepository>()
.RegisterType
<IDeviceInterfaceRepository, DeviceInterfaceRepository>()
.RegisterType
<IMachineAreaRepository, MachineAreaRepository>()
.RegisterType
<ICEService, CEService>()

.RegisterType
<ICEDistributeIpFieldRepository, CEDistributeIpFieldRepository>()
.RegisterType
<ICEDistributeIpFieldService, CEDistributeIpFieldService>();
public static T GetInstance<T>() {
return container.Resolve<T>();
}
}

再次谢谢站长和今朝-TODAY的关注,谢谢你们了。

Atpking的主页 Atpking | 初学一级 | 园豆:150
提问于:2011-07-19 11:03
< >
分享
最佳答案
0

请把Repository中delete和getbyid的实现贴上来。

收获园豆:25
today4king | 老鸟四级 |园豆:3299 | 2011-07-19 23:32
其他回答(1)
0

没有详细的代码,无法分析这个问题。

收获园豆:25
dudu | 园豆:36553 (高人七级) | 2011-07-19 16:58
问题找到了,其实是Model里对另外的一个引用造成的干扰,谢谢站长
支持(0) 反对(0) Atpking | 园豆:150 (初学一级) | 2011-07-21 10:12
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册