T
Thomas Jespersen
Hi
I have a problem getting Linq To SQL to cascade delete OnToMany
relations.
Lets say I have an Order with a collection of OrderLines. I never want
to get or save OrderLines directly... I always want to fetch
OrderLines through the Order.
If I add an OrderLine to an Order, and save the Order. The added
OrderLine gets saved to the database automatically.
But if I remove the OrderLine, it doesn't get deletede in the datbase,
when saving the order.
Can anyone tell me how to get Linq To SQL to cascade delete my
OrderLines?
I already tried DeleteRule = "CASCADE" on the EntitySet<OrderLine> (on
the Order class), but it does not work.
Here are two tests that illustrates the problem (i have at
TestInitialize, which creates an empty database and inserts an order
with 2 order lines).
This test fails on the last assert (the order line is not deleted in
the database when saving the order).
[TestMethod]
public void CanRemoveOrderLine()
{
const int orderId = 1234;
Order order = from o in dataContext.Order where o.Id == orderId
select o).Single();
Assert.AreEqual(2, order.OrderLines.Count(), "There should be 2
order lines on this order.");
//Remove the first orderLine
order.OrderLines.RemoveAt(0);
dataContext.SubmitChanges();
string countOrderLinesSql = String.Format("Select Count(*) From
Order Where OrderId = '{0}'", orderId);
Assert.AreEqual(1, ExecuteScalarSql<int>(countOrderLinesSql),
"There should now only be 1 Order line belonging to the order.");
}
This test however is working! (the new order line is saved when saving
the order)
[TestMethod]
public void CanAddOrderLineToOrder()
{
const int orderId = 1234;
Order order = from o in dataContext.Order where o.Id == orderId
select o).Single();
Assert.AreEqual(2, order.OrderLines.Count(), "There should be 2
order lines on this order.");
//Add a new Order line
var newOrderline = new OrderLine() {Order = order};
order.AddOrderLine(newOrderline);
dataContext.SubmitChanges();
string countOrderLinesSql = String.Format("Select Count(*) From
Order Where OrderId = '{0}'", orderId);
Assert.AreEqual(3, ExecuteScalarSql<int>(countOrderLinesSql),
"There should now be 3 Order line belonging to the order.");
}
My classes look something like this:
public class Order
{
[Column(Name = "Id", IsPrimaryKey = true)]
public Guid OrderId { get; set; }
[Association(OtherKey = "OrderId", DeleteRule = "CASCADE")]
public EntitySet<OrderLine> OrderLines { get; set; }
}
public class OrderLine
{
private EntityRef<Order> order;
[Column] private Guid OrderId;
[Column(Name = "Id", IsPrimaryKey = true)]
public Guid OrderLineId { get; set; }
[Association(Storage = "order", ThisKey = "OrderId")]
public Order Order
{
get { return order.Entity; }
set
{
order.Entity = value;
OrderId = value.OrderId;
}
}
}
Why does adding work, and not deleting?
Any help is much appreciated.
: Thomas
I have a problem getting Linq To SQL to cascade delete OnToMany
relations.
Lets say I have an Order with a collection of OrderLines. I never want
to get or save OrderLines directly... I always want to fetch
OrderLines through the Order.
If I add an OrderLine to an Order, and save the Order. The added
OrderLine gets saved to the database automatically.
But if I remove the OrderLine, it doesn't get deletede in the datbase,
when saving the order.
Can anyone tell me how to get Linq To SQL to cascade delete my
OrderLines?
I already tried DeleteRule = "CASCADE" on the EntitySet<OrderLine> (on
the Order class), but it does not work.
Here are two tests that illustrates the problem (i have at
TestInitialize, which creates an empty database and inserts an order
with 2 order lines).
This test fails on the last assert (the order line is not deleted in
the database when saving the order).
[TestMethod]
public void CanRemoveOrderLine()
{
const int orderId = 1234;
Order order = from o in dataContext.Order where o.Id == orderId
select o).Single();
Assert.AreEqual(2, order.OrderLines.Count(), "There should be 2
order lines on this order.");
//Remove the first orderLine
order.OrderLines.RemoveAt(0);
dataContext.SubmitChanges();
string countOrderLinesSql = String.Format("Select Count(*) From
Order Where OrderId = '{0}'", orderId);
Assert.AreEqual(1, ExecuteScalarSql<int>(countOrderLinesSql),
"There should now only be 1 Order line belonging to the order.");
}
This test however is working! (the new order line is saved when saving
the order)
[TestMethod]
public void CanAddOrderLineToOrder()
{
const int orderId = 1234;
Order order = from o in dataContext.Order where o.Id == orderId
select o).Single();
Assert.AreEqual(2, order.OrderLines.Count(), "There should be 2
order lines on this order.");
//Add a new Order line
var newOrderline = new OrderLine() {Order = order};
order.AddOrderLine(newOrderline);
dataContext.SubmitChanges();
string countOrderLinesSql = String.Format("Select Count(*) From
Order Where OrderId = '{0}'", orderId);
Assert.AreEqual(3, ExecuteScalarSql<int>(countOrderLinesSql),
"There should now be 3 Order line belonging to the order.");
}
My classes look something like this:
public class Order
{
[Column(Name = "Id", IsPrimaryKey = true)]
public Guid OrderId { get; set; }
[Association(OtherKey = "OrderId", DeleteRule = "CASCADE")]
public EntitySet<OrderLine> OrderLines { get; set; }
}
public class OrderLine
{
private EntityRef<Order> order;
[Column] private Guid OrderId;
[Column(Name = "Id", IsPrimaryKey = true)]
public Guid OrderLineId { get; set; }
[Association(Storage = "order", ThisKey = "OrderId")]
public Order Order
{
get { return order.Entity; }
set
{
order.Entity = value;
OrderId = value.OrderId;
}
}
}
Why does adding work, and not deleting?
Any help is much appreciated.
: Thomas