Reputation: 21419
I have been through Breeze documentation regarding navigation properties, cascade deletes and such. I have successfully managed to have one-to-many relationships work as expected with cascade deletes and Breeze. I am running out of ideas as to how the following code is failing, any help would be highly appreciated.
The code below looks similar to Order
and OrderDetail
classes from DocCode Breeze documentation:
public class Order
{
public int OrderId { get; set; }
public String Title { get; set; }
public virtual ICollection<OrderDetail> OrderDetails { get; set; }
}
public class OrderDetail
{
public int OrderDetailId { get; set; }
public String Comment { get; set; }
public int OrderId { get; set; }
[ForeignKey("OrderId ")]
[InverseProperty("OrderDetails ")]
public Order Order { get; set; }
}
public class MyDbContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Use singular table names
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Order>()
.HasMany(t => t.OrderDetails)
.WithRequired(t => t.Order)
.HasForeignKey(t => t.OrderId)
.WillCascadeOnDelete();
base.OnModelCreating(modelBuilder);
}
}
When the Order
entity is removed, I intercept the BeforeSaveChanges
and remove all OrderDetail
objects from the SaveMap
as the DB will take care of deleting child entities.
Problem
When I create OrderDetail
objects, these get correctly added to the DB and my Breeze client successfully manages to read these and associate them correctly. However, when I delete the Order
object by order.entityAspect.setDeleted();
, the children OrderDetails
get sent to the server along with their deleted parent Order
. The issue is that OrderDetails
entities OrderId
properties are set to 0. This raises a big issue as I can't identify which OrderDetail
object to remove from the SaveMap
Upvotes: 1
Views: 214
Reputation: 17052
On the server the EntityInfo.OriginalValuesMap
will have the original values for the deleted order ids.
Upvotes: 1