WPF 应用程序中的 LINQ to SQL 更新问题

发布于 2024-08-13 18:58:40 字数 1191 浏览 1 评论 0原文

我正在开发一个 WPF 应用程序(.NET 3.5SP1)。场景是这样的:

1.使用 SQL Metal 提取实体定义(以及相应的映射文件),随后将其用于数据访问层(所以我使用 LINQ to SQL)。

2.使用Unity(此应用程序是“基于PRISM”)注册抽象类及其相应的实现(例如IRepository和ActualRepository、IDataContext和ActualContext、IUnitOfWork和ActualUnitOfWork等等... - 类名不是真实的,但这在这里并不重要)

3.MVVM风格用于创建ViewModel和View。

插入和删除工作正常..但后来我注意到这种奇怪的行为:

a)通过创建新记录并填写一些字段,用户只能保存此(新)记录一次!如果用户忘记将一些数据插入其他字段并尝试(再次)保存该记录,这些更改不会反映在 dbms 中!

因此,用户关闭了应用程序,再次打开应用程序并加载(之前插入的)记录。现在:对这些记录的每一个更改实际上都反映到了 dbms!

我认为这一定是数据上下文的问题(以这种方式创建:)

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

做了一些谷歌搜索,发现了一些有关分离对象的问题。我不确定这是否与我的情况有关,但阅读了一些博客,我明白这必须与我的情况有关!

长话短说:我想我在表中使用时间戳字段。

b) 说..完成!瞧:新创建的记录可以在第一次保存后保存多次。即使关闭并重新打开应用程序也能正常工作!

但是:出现了一个新问题!

一种视图是使用一些组合框。只要我们正在使用的记录是实际记录,绑定到这些组合框的字段就会正确更新。当我们移动到下一条记录时,先前的当前记录将其字段(绑定到组合框)分配为 null!!!!我认为这是一个绑定问题,并在组合框中明确放置了双向绑定模式,但没有结果。

这让我发疯! (在情况a)我没有遇到这些问题!!!!)

绑定到文本框的字段按预期工作! 顺便说一句:我没有收到任何绑定错误!

那么:有人可以解释一下为什么情况 a) 会这样吗?为什么 b) 绑定到 WPF 组合框的字段会这样?

提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溇涏 2024-08-20 18:58:40

如果您在保存后重用 linq to sql 上下文进行选择,请不要这样做。

作为最佳实践,为插入/更新/删除创建一个新的上下文,包装在 using 中:

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

    dc.SaveChanges();
}

在(重新)绑定时创建另一个新的 DataContext 实例

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文