Guys,
I am using LINQ2SQL and I have a table called Customers with three columns
CustmerID, CustomerCode, CustomerName
CustmerID is Primery Key(and Identity=yes) and CustomerCode is just UniqueKey.
When I am updating this table using LINQ to SQL with duplicate customercode, I expect to see DuplicateKeyException but it is going into the general exception block instead of DuplicateKeyException block. Any ideas?
This is the code
public void Update(Customer cust) { using (LINQDemoDataContext db = new LINQDemoDataContext()) { Customers entity = CustomerMapper.ToEntity(new Customers(), cust);
try
{
db.Customers.Attach(entity, true);
db.SubmitChanges();
}
//Concurrency Exception
catch (ChangeConflictException)
{
throw new ChangeConflictException("A concurrency error occurred!");
}
//duplicate record
catch (DuplicateKeyException)
{
throw new DuplicateKeyException(entity.CustmerCode);
}
//everything else
catch (Exception ex)
{
throw ex;
}
}
}
I am using VisualWebDeveloperExpress 2008 and SQL Express 2005.
Thanks & Regards, Supremestar