SubmitChanges 未“提交”在 LINQ to SQL 中
我正在使用 WPF、MVVM 和 LINQ to SQL 创建一个应用程序。我有一个关于计算类型对象的注释集合。因此,我为此创建了一个名为 vmCalculation 的 ViewModel 类。我的问题是,当我尝试向该对象添加注释并提交更改时,“注释”不会提交到数据库。
vmNotes 中vmCalculation 方法的内容
public class vmCalculation : vmBase
{
Calculation calc;
public ObservableCollection<Note> Notes { get; private set; }
public vmCalculation(Calculation calc)
{
this.calc = calc;
Notes = new ObservableCollection<Note>();
foreach (var n in calc.Notes) Notes.Add(n);
}
public void AddNote()
{
Notes.Add(new Note
{
NoteText = "New note",
NoteType = 1
});
}
internal void Save()
{
foreach (var n in Notes.Where(n => n.NoteId == 0))
calc.Notes.Add(n);
}
}
(“NoteWindow”的 ViewModel)
public void SaveChanges()
{
CurrentCalc.Save();
DC.SubmitChanges();
}
CurrentCalc 是一个属性,用于获取/设置我在数据绑定中使用的 vmCalculation(将 DataGrid 绑定到 CurrentCalc.Notes)。
当我在 CurrentCalc 上运行 AddNote() 时,视图会通过“新注释”注释更新得很好。但是,当我运行 SaveChanges() 时,注释不会写入数据库。
对这个问题有什么想法吗?
该问题的一个可能原因是我没有在 vmNotes 中初始化 DataContext (DC)。我从另一个 ViewModel 获取 DataContext,这样我就不会破坏 MVVM 结构。
I'm creating an application using WPF, MVVM and LINQ to SQL. I have a collection of notes on an object of type Calculation. I have therefore created a ViewModel-class for this called vmCalculation. My problem is, that when I try to add a note to this object and submit the changes the "note" isn't submittet to the database.
Content of vmCalculation
public class vmCalculation : vmBase
{
Calculation calc;
public ObservableCollection<Note> Notes { get; private set; }
public vmCalculation(Calculation calc)
{
this.calc = calc;
Notes = new ObservableCollection<Note>();
foreach (var n in calc.Notes) Notes.Add(n);
}
public void AddNote()
{
Notes.Add(new Note
{
NoteText = "New note",
NoteType = 1
});
}
internal void Save()
{
foreach (var n in Notes.Where(n => n.NoteId == 0))
calc.Notes.Add(n);
}
}
Method in vmNotes (ViewModel for the "NoteWindow")
public void SaveChanges()
{
CurrentCalc.Save();
DC.SubmitChanges();
}
CurrentCalc is a property that gets/sets a vmCalculation that I use in the databinding (binding a DataGrid to CurrentCalc.Notes).
When I run AddNote() on CurrentCalc the view is updated just fine with a "New note"-note. But, when I run SaveChanges() the note isn't written to the database.
Any thoughts on this problem?
A possible cause for the the problem could be, that I don't initialize the DataContext (DC) in vmNotes. I get the DataContext from another ViewModel so that I don't destroy the MVVM-structure.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在提交之前将新实体添加到数据上下文中。
例子:
You must add your new entities to the datacontext before you submit it.
Example:
想到了解决我的问题的可能方法。
我稍微更新了 vmNotes 类上的 SaveChanges() 方法。
更新 03/09/2011:
无论如何都不需要上面的代码。
我发现我的 DataModel 类有多个(静态)实例。
我删除了其中一些,现在我的原始代码工作得很好!
Thought of a possible solution for my problem.
I updated the SaveChanges()-method on the vmNotes class a bit.
UPDATE 03/09/2011:
Above code is not needed anyway.
I discovered that I had multiple (and static) instances of my DataModel-class.
I cut away some of these and now my original code works just fine!