Hi There,
I have been playing around with DLinq getting it to apply changes i make to objects in code back to the DB. I understand that by default it uses optimistic concurrency, and this is fine for my purposes however when a conflict does arise is there any way of finding out what entity/row the conflict occurred on
So here is my code - it all works fine but if I modify a customer in the DB while my project has a copy and a concurrency issue arrises submit changes throws an exception as expected, but I have no way of knowing which object (in this case customer) the conflict occurred. Is there any way to find out other than checking each customer object against the db manually in code
Thanks.
// Use a standard connection string DataContext db = new DataContext(@"server=.;UID=sa;Password=*******;AttachDbFilename=C:\Program Files\LINQ Preview\Data\northwnd.mdf;database=Northwind");db.Log =
Console.Out; // Get a typed table to run queries Table<Customer> Customers = db.GetTable<Customer>(); Console.WriteLine("Contact Count = " + Customers.Count<Customer>().ToString()); Console.WriteLine("Please enter the city you would like to filter by:"); var City = Console.ReadLine(); var LondonersQ = from C in Customers whereC.City == City || City.Length == 0
select C;var Londoners = LondonersQ.ToList<Customer>(); foreach(Customer Cust in Londoners)
{
Console.WriteLine("{0} {1}",Cust.CustomerID,Cust.City);}
Console.WriteLine("Please enter the city you would like to move these customers to:"); var NewCity = Console.ReadLine(); if(NewCity.Length > 0){
foreach(Customer Cust in Londoners){
Cust.City = NewCity;
}
Console.WriteLine("Submitting Changes."); try{
db.SubmitChanges(
ConflictMode.FailOnFirstConflict); Console.WriteLine("Changes Submitted!");}
catch(Exception ex){
Console.WriteLine(ex.ToString());}
}
else Console.WriteLine("City left as is."); Console.ReadLine();
DLinq - Concurrency Check failure - how do I find out what it failed on?
M Thomas
rekan
This is covered in section 7.7.5 of the DLinq overview documents (in the May CTP). Below is one of the examples from that document. In short, issue the SubmitChanges method stating to ContinueOnConflict as the ConflictMode. Then, you can catch an OptimisticConcurrencyException and interrogate it for the table name, identity column value, and other aspects as necessary.
Example 4
This example shows a way of accessing information on an entity in conflict.
try {
user1.SubmitChanges(ConflictMode.ContinueOnConflict);
}
catch (OptimisticConcurrencyException e) {
Console.WriteLine("Optimistic concurrency error");
Console.ReadLine();
foreach (OptimisticConcurrencyConflict cc in e.Conflicts) {
ITable table = cc.Table;
Customers entityInConflict = (Customers)cc.Object;
Console.WriteLine("Table name: {0}", table.Name);
Console.Write("Customer ID: ");
Console.WriteLine(entityInConflict.CustomerID);
}
}
Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx