开发者

LINQ to SQL update problems in WPF application

I'm developing a WPF application (.NET 3.5SP1).开发者_C百科 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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜