什么可能导致 db.SubmitChanges() 在 linq-to-sql 中不起作用?
我在 WPF 中使用 LINQ-TO-SQL 设置了一个非常简单的示例。
我可以像这样得到一个对象(pageItem),我可以更改属性,当我调用SubmitChanges()时,它会给我 >没有错误,但它不保存更改。
MainDataContext db = new MainDataContext();
var pageItem = (from p in db.PageItems
where p.Id == 1
select p).SingleOrDefault();
pageItem.Title = "changed";
db.SubmitChanges();
什么可能导致 SubmitChanges 不提交更改?
更多信息:
这也不起作用,甚至 db.ExecuteCommand 也不起作用,奇怪的是,当调试 F11 时,它没有进入 SubmitChanges() 或 ExecuteCommand(),为什么我不能进入这些?
using (var db = new MainDataContext())
{
var pageItem = (from p in db.PageItems
where p.Id == 1
select p).SingleOrDefault();
pageItem.Title = "changed";
db.SubmitChanges();
db.ExecuteCommand("INSERT INTO PageItems (Title) VALUES ('this is the title')");
if (pageItem != null)
MainContent.Children.Add(new QuickForm(pageItem));
}
更多信息:
db.Log = Console.Out 给了我这个:
SELECT [t0].[Id], [t0].[IdCode], [t0].[Title], [t0].[Description], [t0].[DisplayOrder]
FROM [dbo].[PageItems] AS [t0]
WHERE [t0].[Id] = @p0
'TestPageManager23434.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\PresentationFramework.resources\3.0.0.0_de_31bf3856ad364e35\PresentationFramework.resources.dll'
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
INSERT INTO PageItems (Title) VALUES ('this is the title')
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
The thread 0x1190 has exited with code 0 (0x0).
答案
解决方案有三部分:
我正在更改与在 Visual Studio 中查看的数据库不同的数据库,解决方案:
var db = new MainDataContext(@"C:\Users\TestUser\Documents\Visual Studio 2008\Projects\TestPageManager23434\TestPageManager23434\Data\Main.mdf"))
进行了更新工作但不能 SubmitChanges(),解决方案是设置主键。
它仍然没有显示所有的chagnes,问题是我打开了许多“显示数据”窗口,但没有更新
I've set up a very simple example with LINQ-TO-SQL in WPF.
I can get an object (pageItem) out like this and I can change the property and when I call SubmitChanges() it gives me no error but it doesn't save the change.
MainDataContext db = new MainDataContext();
var pageItem = (from p in db.PageItems
where p.Id == 1
select p).SingleOrDefault();
pageItem.Title = "changed";
db.SubmitChanges();
What could be causing SubmitChanges not to submit the changes?
MORE INFO:
This doesn't work either, even the db.ExecuteCommand doesn't work, and strangely when debugging F11 doesn't step into SubmitChanges() or ExecuteCommand(), why can't I step in those?
using (var db = new MainDataContext())
{
var pageItem = (from p in db.PageItems
where p.Id == 1
select p).SingleOrDefault();
pageItem.Title = "changed";
db.SubmitChanges();
db.ExecuteCommand("INSERT INTO PageItems (Title) VALUES ('this is the title')");
if (pageItem != null)
MainContent.Children.Add(new QuickForm(pageItem));
}
more info:
The db.Log = Console.Out gives me this:
SELECT [t0].[Id], [t0].[IdCode], [t0].[Title], [t0].[Description], [t0].[DisplayOrder]
FROM [dbo].[PageItems] AS [t0]
WHERE [t0].[Id] = @p0
'TestPageManager23434.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\PresentationFramework.resources\3.0.0.0_de_31bf3856ad364e35\PresentationFramework.resources.dll'
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
INSERT INTO PageItems (Title) VALUES ('this is the title')
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
The thread 0x1190 has exited with code 0 (0x0).
ANSWER
The solution was three-fold:
I was changing a different database than I was looking at in visual studio, solution:
var db = new MainDataContext(@"C:\Users\TestUser\Documents\Visual Studio 2008\Projects\TestPageManager23434\TestPageManager23434\Data\Main.mdf"))
that made Update work but not SubmitChanges(), solution was to set the primary key.
it still wasn't showing all the chagnes, problem was I had a number of "show data" windows open which weren't being updated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您没有在 SQL Server 中的表上定义主键,则可能会发生这种情况
This can happen if you don't have a primary key defined on the tables in SQL Server
由于某种原因,上下文可能无法跟踪更改。尝试将您的
db.Log
连接到编写器,并检查当您调用SubmitChanges()
时LINQ->SQL正在做什么。然后您可以观察输出窗口的运行情况在调试中看看发生了什么。
For some reason the context may not be tracking changes. Try wiring up your
db.Log
to a writer and inspect what LINQ->SQL is doing when you callSubmitChanges()
..Then you can watch your output window running in debug and see what is going on.
您使用 SQL 表达式 mdf 文件吗?
有一篇文章 关于这可能如何导致您获得文件的副本而不是原始文件,从而导致您所描述的症状。
自由贸易协定:
Are you using SQL Expression mdf file?
There's an article about how this might cause you to get a copy of the file and not the original, causing the symptoms you're describing.
FTA:
我遇到了同样的问题,我在项目中看到的数据库中的记录未被
SubmitChanges
方法修改。经过多次尝试和研究,我发现系统将另一个版本的数据库
Northwnd.mdf
放在项目的根目录\Bin\Debug\Northwnd中。中密度纤维板
。这就是完美发生变化的地方。I had the same problem in which the record in the Database I see in my project was not modified by the
SubmitChanges
method.After so many trial and researches I found out that the system put another version of the Database
Northwnd.mdf
in the project's root directory\Bin\Debug\Northwnd.mdf
. That is where the changes, perfectly, occurred.