C# EF 6.1.1中的孤立子对象问题
我有以下结构的域对象C# EF 6.1.1中的孤立子对象问题,c#,.net,asp.net-mvc,entity-framework,ef-code-first,C#,.net,Asp.net Mvc,Entity Framework,Ef Code First,我有以下结构的域对象 public class Country : Entity { public Country() { States=new List<State>(); } public string CountryName { get; set; } public string CountyCode { get; set; } public
public class Country : Entity
{
public Country()
{
States=new List<State>();
}
public string CountryName { get; set; }
public string CountyCode { get; set; }
public virtual ICollection<State> States { get; private set; }
public State GetStateById(int stateId)
{
return States.FirstOrDefault(x => x.Id == stateId);
}
public void DeleteState(int stateId)
{
var state = GetStateById(stateId);
if(state==null) return;
States.Remove(state);
}
}
公共类国家/地区:实体
{
公共国家()
{
状态=新列表();
}
公共字符串CountryName{get;set;}
公共字符串CountyCode{get;set;}
公共虚拟ICollection状态{get;private set;}
公共状态GetStateById(int stateId)
{
返回States.FirstOrDefault(x=>x.Id==stateId);
}
公共void DeleteState(int stateId)
{
var state=GetStateById(stateId);
if(state==null)返回;
移除(状态);
}
}
这里我使用DeleteState方法删除一个state对象(它是一个国家的子对象)
我有一个具有以下结构的存储库(它是一个聚合根)
public class CountryRepository : Repository<Country>, ICountryRepository
{
public CountryRepository(IErpBaseUnitOfWork unitOfWork)
: base(unitOfWork)
{
}
// Unnecessary codes removed
public void Modify<TEntity>(TEntity item)
where TEntity : class
{
//this operation also attach item in object state manager
Entry<TEntity>(item).State = EntityState.Modified;
}
}
公共类CountryRepository:Repository,ICONTRYRepository
{
公共国家存储库(IErpBaseUnitOfWork unitOfWork)
:基础(工作单元)
{
}
//删除不必要的代码
公共无效修改(临时项)
地点:班级
{
//此操作还将附加对象状态管理器中的项
条目(项).State=EntityState.Modified;
}
}
和unitofwork.commit调用上下文.SaveChanges
业务层
public class CountryAppService : ICountryAppService
{
private readonly ICountryRepository _countryRepository;
public CountryAppService(ICountryRepository countryRepository)
{
_countryRepository = countryRepository;
}
public CountryDto RemoveState(int stateId, int countryid)
{
var country = _countryRepository.FindById(countryid);
if (country == null || country.Status == false) throw new ApplicationOperationException(Messages.Validation_CountryInInvalidState) { HttpCode = 400 };
country.DeleteState(stateId);
_countryRepository.Modify(country);
_countryRepository.UnitOfWork.Commit();
return country.ProjectedAs<CountryDto>();
}
}
公共类CountryAppService:IContryAppService
{
私有只读ICountryRepository\u countryRepository;
公共CountryAppService(ICountryRepository countryRepository)
{
_countryRepository=countryRepository;
}
要重新投资的公共国家(int stateId,int countryid)
{
var country=\u countryRepository.FindById(countryid);
如果(country==null | | country.Status==false)抛出新的ApplicationOperationException(Messages.Validation_CountryInInvalidState){HttpCode=400};
country.DeleteState(stateId);
_countryRepository.Modify(国家);
_countryRepository.UnitOfWork.Commit();
返回国家/地区。ProjectedAs();
}
}
在这里,我通过从集合中删除对象来删除状态对象,但是由于存在孤立状态,EF将生成一个错误,我的问题是如何通过从集合中删除来删除状态。考虑的要点
1。)我的域模型对EF或其相关技术没有任何线索,因此在域层和业务层中包含少量与EF相关的代码不是一个好主意
2.)我将Db上下文与CodeFirst一起使用,
处理这类情况的最佳做法是什么
使用fluent api,您可以告诉EF当主体被删除时孤儿被删除
尽管如此,EF并不总是允许在删除时级联。由于复杂的关系
在这种情况下,可以
将删除子项放入DAL层逻辑中。
如果从域模型的角度来看,delete Main意味着删除所有子项,我会这样做
这项服务是预期的/需要的
然而,数据的关联方式通常被视为域层主题
我将Delete子项放在我个人在域层的业务逻辑中。
如果域层对此进行建模并调用触发了子删除,我认为没有问题。
但是,我已经看到了位于域层之上的服务层/业务层,并且没有任何问题。我只是把它们当作整体设计的一部分。当然,当服务/业务层负责协调相关的删除时,您间接地将域层链接到了服务层。域完整性在哪里
最佳实践取决于您定义域层的方式。
这不是一个真正的问题。这是一个非常大的设计主题问题。
也许可以开始阅读DDD
您解决过这个问题吗?