I have a 1..*
relationship between X
and Y
, where X
is the parent. When I try and delete record Y
I get the following exception message:
Entities in 'Y' participate in the 'FK_Y_X' relationship. 0 related 'X' were found. 1 'X' is expected.
I am currently trying to delete the record in a generic, disconnected manner in the following way:
public bool Delete(TEntity entity)
{
if (entity == null)
{
return false;
}
try
{
var entry = _context.Entry(entity);
entry.State = EntityState.Deleted;
_context.SaveChanges();
return true;
}
catch
{
return false;
}
}
The entity that is passed in is loaded with AsNoTracking()
on the same context.
Any ideas?
Try to add a public property YId to X which will hold the connection to Y, this solved my issue, i use Breeze with EF6 and i've got the same error.
Class Y
{
public int Id { get; set; }
public ICollection<X> Xs { get; set; }
}
Class X
{
public int Id { get; set; }
public int YId { get; set; }
public Y Y { get; set; }
}
When you context.Y.Attach(x);
make sure that the object x
holds the "Id" to the parent entity. You need both.
Example:
x.Id = 3;
x.ParentEntity.Id = 10;
That solved my problem at least. I forgot the parent id in the object which i attached.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With