简体   繁体   English

SubmitChanges不是在LINQ to SQL中“提交”

[英]SubmitChanges not “submitting” in LINQ to SQL

I'm creating an application using WPF, MVVM and LINQ to SQL. 我正在使用WPF,MVVM和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. 因此,我为此创建了一个名为vmCalculation的ViewModel类。 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 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") vmNotes中的方法(“NoteWindow”的ViewModel)

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). CurrentCalc是一个获取/设置我在数据绑定中使用的vmCalculation的属性(将DataGrid绑定到CurrentCalc.Notes)。

When I run AddNote() on CurrentCalc the view is updated just fine with a "New note"-note. 当我在CurrentCalc上运行AddNote()时,使用“新笔记”-note更新视图。 But, when I run SaveChanges() the note isn't written to the database. 但是,当我运行SaveChanges()时,注释不会写入数据库。

Any thoughts on this problem? 有关这个问题的任何想法?

A possible cause for the the problem could be, that I don't initialize the DataContext (DC) in vmNotes. 问题的可能原因可能是,我没有在vmNotes中初始化DataContext(DC)。 I get the DataContext from another ViewModel so that I don't destroy the MVVM-structure. 我从另一个ViewModel获取DataContext,这样我就不会破坏MVVM结构。

You must add your new entities to the datacontext before you submit it. 在提交之前,必须将新实体添加到datacontext。 Example: 例:

DC.Notes.InsertOnSubmit(NewNote);
DC.SubmitChanges();

Thought of a possible solution for my problem. 想到我的问题的可能解决方案。

I updated the SaveChanges()-method on the vmNotes class a bit. 我稍微更新了vmNotes类上的SaveChanges()方法。

public void SaveChanges()
    {
        var newNotes = currentCalc.Notes.Where(n => n.NoteId == 0);
        DC.Notes.InsertAllOnSubmit(newNotes);
        DC.SubmitChanges();
    }
}

UPDATE 03/09/2011: 更新03/09/2011:

Above code is not needed anyway. 无论如何,上面的代码都不需要。

I discovered that I had multiple (and static) instances of my DataModel-class. 我发现我有DataModel类的多个(和静态)实例。

I cut away some of these and now my original code works just fine! 我删除了其中的一些,现在我的原始代码工作得很好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM