Reputation: 11
I have the following issue which drove me crazy during the last days. Now I found a workaround - but I like to search for the root cause.
I have an entity OrderItemAdjustment defined like this:
this.HasMany(oia => oia.Adjustments).WithRequired(a => a.OrderItemAttribute).HasForeignKey(a => a.OrderItemAttributeId);
No I'm trying to save a record:
var orderAttr = (from oa in _context.OrderAttributes where oa.Id = 4711 select oa).FirstOrDefault();
var itemAttr = orderAttr.ItemAttributes.FirstOrDefault(ia => ia.OrderItemId == 4712);
var oia = new OrderItemAdjustment()
{
AdjustmentId = 1
}
itemAttr.Adjustments.Add(oia);
_context.SaveChanges();
In this case the entity OrderItemAdjustment is not saved. When I load the entity directly like this and replace line 3 it works:
var itemAttr = (from oia in _context.OrderItemAttributes where oia = 4712 select oia).FirstOrDefault();
Can somebody explain to me why EF behaves like that - especialy just not saving without giving any error?
Kind Regards Jörg
Upvotes: 1
Views: 64
Reputation: 157
well, you should add your new object in your context before calling _context.savechanges(). so it looks like this
var oia = new OrderItemAdjustment()
{
AdustementId = 1
}
_Context.add(oia)
_Context.SaveChanges().
Upvotes: 1