I have a problem to save changes to database when updating entity where multiple children of collection are removed in method of entity.
I call RemoveSpecificMiddleLvlEntities
method from HighLvlEntity
entity. Some Children are deleted. When updating through Update method from EfRepository
following error is thrown.
Do i have to manually all levels of children in the collections through my Delete method?
I think i don't really get one of the basisc Entity Framework 6 concepts? Wanted to have my logic which entities are deleated in my Entity not in any repository or service.
Thank you in advance.
Error: System.InvalidOperationException: "The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted."
My simplified entities:
public class Base
{
public Guid Id { get; set; }
public Base()
{
Id = Guid.NewGuid();
}
}
public class HighLvlEntity : Base
{
public HighLvlEntity(string name)
{
Name = name;
}
public string Name { get; set; }
public ICollection<MiddleLvlEntity> middleLvlEntities { get; set; } = new List<MiddleLvlEntity>();
public void RemoveSpecificMiddleLvlEntities(ICollection<MiddleLvlEntity> middleLvlEntitiesOfCriteria)
{
foreach (var MiddleLvlEntity in middleLvlEntitiesOfCriteria)
{
var foundmiddleLvlEntity = middleLvlEntities.Where(p => p.Testcriteria == MiddleLvlEntity.Testcriteria).First();
middleLvlEntities.Remove(foundmiddleLvlEntity);
{
}
}
}
}
public class MiddleLvlEntity : Base
{
public MiddleLvlEntity(string testcriteria)
{
Testcriteria = testcriteria;
}
public ICollection<LowLvlTwo> LowLvlTwos { get; set; } = new List<LowLvlTwo>();
public ICollection<LowLvlOne> LowLvlOnes { get; set; } = new List<LowLvlOne>();
public string Testcriteria { get; set; }
public HighLvlEntity HighLvlEntity { get; set; }
public Guid HighlvlEntityId { get; set; }
}
public class LowLvlOne : Base
{
public LowLvlOne(DateTime date)
{
Date = date;
}
public DateTime Date { get; set; }
public Guid middleLvlEntityId { get; set; }
public MiddleLvlEntity MiddleLvlEntity { get; set; }
}
public class LowLvlTwo : Base
{
public LowLvlTwo(DateTime date)
{
Date = date;
}
public DateTime Date { get; set; }
public Guid middleLvlEntityId { get; set; }
public MiddleLvlEntity MiddleLvlEntity { get; set; }
}
EfRepository:
public class EfRepository<T> : IRepository<T> where T : Base
{
protected readonly Context _dbContext;
public EfRepository(Context dbContext)
{
_dbContext = dbContext;
}
public void Update(T entity)
{
_dbContext.Entry(entity).State = EntitieState.Modified;
_dbContext.SaveChanges();
}
public void Delete(T entity)
{
_dbContext.Set<T>().Remove(entity);
_dbContext.SaveChanges();
}
}
Update: This seems to be same problem. Then my question is, how the hell i can architecture my application that it works. I have a Core project with entities, interfaces and services. An Infrastructure project with implementation and connection to Entity Framework. So my entities in Core project don't know about dbcontext and removing items from childcollections. Do i have to move from the idea to implement business logic in the entity itself to use service-interfaces in Core project and service implementations in Infrastructure project?