> I've never been able to work out the DbLinq way to do the create,
update and delete operations and ended up implementing the dynamic
methods in the DataContext class which work well for me.
I'm using dbLinq for insert, update,delete in web service called from
silverlight using code below.
Transactions are not working as described in other thread. Any idea how to
use transactions ?
Andrus.
/// <summary>
/// Performs insert, update,delete operations or changed and original entity
lists
/// </summary>
public static void Save<TEntity>(List<TEntity> orig, List<TEntity> modif) {
int inserted = 0;
using (TransactionScope tran = new TransactionScope()) // TODO: explore why
this is ignored
using (var db = new DataContext())
{
var table = db.GetTable<TEntity>();
// Update and insert
foreach (TEntity modified in modif)
{
bool match = false;
foreach (TEntity original in orig)
{
//if the record exis in both collections then is an update
if (modified.PrimaryKeysEqual(original))
{
//LINQ to SQL will take care for the optimistic locking check
table.Attach(modified, original);
match = true;
break;
}
}
//if a record exist only in the modifieds collection it means is an
insert
if (!match)
{
inserted++;
table.InsertOnSubmit(modified);
}
}
//check if there are deleted records
if (orig != null && orig.Count > modif.Count - inserted)
{
foreach (TEntity original in orig)
{
bool match = false;
foreach (TEntity modified in modif)
{
//check if the record exist only in the originals collection
if (modified.PrimaryKeysEqual(original))
{
match = true;
break;
}
}
//if dosent have a modified counterpart it means that is a delete
if (!match)
{
table.Attach(original);
table.DeleteOnSubmit(original);
}
}
}
//save the changes on the the DB
table.Context.SubmitChanges();
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"DbLinq" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/dblinq?hl=en
-~----------~----~----~----~------~----~------~--~---