views:

275

answers:

1

Hi everybody! I'm developing a WPF application (.NET 3.5SP1). The scenario is this:

1.using SQL Metal to extract the entity definitions (and the corresponding map file), which were afterwards used for the Data Access Layer (so i'm using LINQ to SQL).

2.using Unity (this application is "PRISM based") to register abstract classes and their corresponding implementations (e.g. IRepository and ActualRepository, IDataContext and ActualContext, IUnitOfWork and ActualUnitOfWork, and so on... - the class names are not the real ones, but this is not important here)

3.MVVM style is used to create the ViewModel and the View.

Inserting and deleting worked fine .. BUT then i noticed this weird behaviour:

a) by creating a new record and filling out some fields the user was able to save this (new) record only ONCE!!! If the user had forgotten to insert some data into other fields and tried to save this record (AGAIN), these changes were not reflected in the dbms!!!

So the user closed the application, opened the application again and loaded (the previously inserted) records. Now: every change to these records WERE actually reflected to the dbms!!!

I thought it has to be an issue with the datacontext (which was created this way:

public SQLDataContext(string connectionString)
        {
            dc = new DataContext(connectionString, Mapping.GetMapping());
        }

)

I did some googling and found some issues regarding detached objects. I wasn't sure if this has something to do with my situation but reading some blogs i understood that it had to!

Long story short: i thought i use a timestamp field in my tables.

b) Said..done!Et voila: the newly created record could be saved more then once after the first save. Even closing and reopening the appliation worked fine!

BUT: a new problem araised!

One view is using some comboboxes. The fields bound to these comboboxes get updated properly as long as this record we are playing with is the actual record. When we move to the next record the previously current record gets its fields (bound to comboboxes) assigned to null!!!! I thought it's a binding issue and exlpicitly placed a two way binding mode in the comboboxes, with no result.

This drives me crazy!!! (in situation a) i did not experience these problem!!!!)

The fields bound to TextBoxes work as expected! BTW: i don't get any binding error!

So: could someone please explain why situation a) is behaving like this? and why b)the fields bound to WPF comboboxes behave like this?

Thanks in advance

A: 

If you're reusing the linq to sql context for selecting after saving, don't do that.

As a best practice, create a new context for your insert/update/delete, wrapped in a using:

using(var dc = new DataContext())
{
    // Delete/Change/Insert some objects

    dc.SaveChanges();
}

Create another new DataContext instance when (re)binding

Sander Rijken
The problem hereby is that i create the Datacontext this way:using (IUnitOfWork unitOfWork = UnitOfWork.Begin()){ unitOfWork.Commit();}and UnitOfWork.Begin() is defined this way:public static IUnitOfWork Begin(){ return ServiceLocator.Current.GetInstance<IUnitOfWork>();}As i've stated above i'm using unity. So what i'm afraid is that Unity returns always the same context!!
Savvas Sopiadis
Are you using separate units of work for saving and fetching?
Sander Rijken
I think i found a solution to my problem (after googling a little bit more):a)timestamp fields removedb)saving a record involves a check (after UnitOfWork.Begin()):if (newRecord) { add(object)} else {refresh(object)}; commitWork();Hope this is fixed!But i still have this issue with the comboboxes!!
Savvas Sopiadis
maybe it's better to create a new question for the comboboxes
Sander Rijken