问题描述
我在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中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!