views:

187

answers:

2

I have a deeper question regarding debug functionality of Linq to Sql SubmitChanges() Function.

I want to save a record in a table of a locally cached db (localdbcache: server SqlExpress 2008 client SqlCE). Before calling SubmitChanges I can find the new item via DataContext.GetChangeSet(). After calling Submit Changes, the items to insert have been removed from the ChangeSet. (That's what this function is supposed to do.) There are no Changes Conflicts and no error in the db's log output. No Exception at all. The table's Count stays at the same value.

if ((e.Parameter == null) ||
   (!e.Parameter.GetType().Equals(typeof(LibDB.Client.Vehicles))))
{
    return;
}

LibDB.Client.Vehicles tmp = e.Parameter as LibDB.Client.Vehicles;

try
{
    ChangeSet cs = this._dc.GetChangeSet();

    if ((tmp == null) || (this._dc == null)) return;

    if (this._dc.Vehicles.Where(veh => veh.Vin == tmp.Vin).Count() == 0)
        this._dc.Vehicles.InsertOnSubmit(tmp);
    else if (this._dc.Vehicles.Where(veh => veh.Vin == tmp.Vin).Count() == 1)
        this._dc.Vehicles.Attach(tmp, true);
    else
        return;

    using (TransactionScope ts = new TransactionScope())
    {
        try
        {                        
            this._dc.SubmitChanges();
            //this._dc.Refresh(RefreshMode.OverwriteCurrentValues,
            //    this._dc.Vehicles);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    if (this._dc.Vehicles.Where(veh => veh.Vin == tmp.Vin).Count() == 1)
        MessageBox.Show("Vehicle not saved.");

    this.vehSelector.ResetLayout();
}

I would appreciate any help since I'm loosing hope to find any error, Thanks in Advance Winston

A: 

Does your entity have a primary key defined in the db?

ifwdev
Yes I have a primary key, it's a manually set Primary Guid Key without setting the acssociation to "IsDbGenerated" because I use a mapping xml file.
winston schröder
A: 

Check the count on a new instance of _dc. You're supposed to dispose it after calling .SaveChanges()

Another way to isolate the problem, is to take a look at the count on the table itself, without using Linq.

Sander Rijken
Well I'm working only on one instance of my Datacontext (_dc) therefore updates usually worked before and then there is no need of detaching entities before adding them. I don't use SaveChanges but I had a look at the Count during debug (if you call a method by "add watch" this method is executed instantanious once, so I added the this._dc.Vehicles.Count() Watch twice. One before, one after calling SubmitChanges().
winston schröder
a DataContext is designed to be used for just a single (set) of actions, it's OK to delete a few, update some others and insert some objects, call SaveChanges. But after saving the changes you should dispose the context and open a new one. It's a cheap object that shouldn't be kept around.
Sander Rijken