Hello All
Im attempting to parse a datasource into individual entities and inserting those into a database. However, some of those entities might already exist so I need to check my primary keys.
I failed at my first attempt - trying to simply catch the exception thrown when the PK constraint is violated.
However, on any consecutive calls of dc.SubmitChanges the exception is still thrown. Obviously not the solution. (Is there a way to discard those changes that cause an exception?)
So my next option is checking whether the entity already exists. Preferably in a single operation. Does Linq provide this? Currently Im entertaining the idea of creating a static, synchronised method which checks the existence before inserting but will this work across page requests? i.e. when the same page is accessed by two or more requests concurrently do they access the same, static method or do the requests live in seperate spaces ?
i.e. would this work and ensure an atomic operation?
thanks all
Im attempting to parse a datasource into individual entities and inserting those into a database. However, some of those entities might already exist so I need to check my primary keys.
I failed at my first attempt - trying to simply catch the exception thrown when the PK constraint is violated.
Code:
try { dc.SubmitChanges(); }
catch { }
However, on any consecutive calls of dc.SubmitChanges the exception is still thrown. Obviously not the solution. (Is there a way to discard those changes that cause an exception?)
So my next option is checking whether the entity already exists. Preferably in a single operation. Does Linq provide this? Currently Im entertaining the idea of creating a static, synchronised method which checks the existence before inserting but will this work across page requests? i.e. when the same page is accessed by two or more requests concurrently do they access the same, static method or do the requests live in seperate spaces ?
i.e. would this work and ensure an atomic operation?
Code:
[MethodImpl(MethodImplOptions.Synchronized)]
public static void InsertQuote(Quote q)
{
if (q != null)
{
if (dc.Quotes.Select(x => x.id == q.id).Count() == 0)
{
dc.Quotes.InsertOnSubmit(q);
dc.SubmitChanges();
}
}
}
thanks all