为什么我的 SubmitChanges() 在 LINQ-to-SQL 中不起作用? [英] Why isn't my SubmitChanges() working in LINQ-to-SQL?

查看:18
本文介绍了为什么我的 SubmitChanges() 在 LINQ-to-SQL 中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WPF 应用程序中创建了一个 .MDF 数据库.

I created a .MDF database in my WPF application.

然后我生成了 LINQ-to-SQL 类并使用 LINQ 获取所有客户.

I then generated LINQ-to-SQL classes and used LINQ get all the customers.

然后我浏览了他们并更改了他们的每个姓氏.

Then I run through them and change each of their last names.

但是,当我调用 SubmitChanges 时,数据库保持不变.

However, when I call SubmitChanges, the database remains unchanged.

我认为这是 SubmitChanges() 的目的,提交对数据库的更改?

I thought that was the purpose of SubmitChanges(), to submit changes to the database?

我遗漏了什么,如何提交更改"回我的数据库?

What am I missing, how do I "submit changes" back to my database?

public Window1()
{
    InitializeComponent();

    Main2DataContext _db = new Main2DataContext();
    var customers = from c in _db.Customers
                    select c;

    foreach (var customer in customers)
    {
        customer.LastName = "CHANGED lastname"; //ListBox shows changes 
    }

    _db.SubmitChanges(); //does NOT save to database (???)

}

推荐答案

我想我知道问题出在哪里了.您使用的是本地 .mdf 文件吗?如果是这种情况,请检查您的 bin/debug 文件夹.我敢打赌,您在那里有一个包含更改的数据库.我认为您的项目中有一个 .mdf 文件,每次构建时都会将其复制到 bin/debug 文件夹中.因此,更改将保存到副本中,而您没有看到它反映在直接驻留在项目中的副本中.

I think I know what the problem is. Are you using a local .mdf file? If that's the case, check your bin/debug folder. I bet you have a database in there with the changes. I think you have an .mdf file in your project that is getting copied to the bin/debug folder everytime you build. Therefore the changes are getting saved to the copy, and you're not seeing it reflected in the copy that resides directly in your project.

这篇关于为什么我的 SubmitChanges() 在 LINQ-to-SQL 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆