SubmitChanges不“提交”在LINQ to SQL中 [英] SubmitChanges not "submitting" in LINQ to SQL

查看:161
本文介绍了SubmitChanges不“提交”在LINQ to SQL中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WPF,MVVM和LINQ to SQL创建一个应用程序。我有一个类型计算对象的笔记集合。因此,我为此创建了一个名为vmCalculation的ViewModel类。我的问题是,当我尝试向该对象添加注释并提交更改时,注释不会提交到数据库。



vmCalculation的内容

  public class vmCalculation:vmBase 
{
计算calc;

public ObservableCollection<注意>注意{get;私人集合

public vmCalculation(Calculation calc)
{
this.calc = calc;
Notes = new ObservableCollection< Note>();
foreach(在calc.Notes中为var n)Notes.Add(n);
}

public void AddNote()
{
Notes.Add(new Note
{
NoteText =New note,
NoteType = 1
});
}

internal void Save()
{
foreach(在Notes.Where(n => n.NoteId == 0)中的var n)
calc.Notes.Add(n);
}
}

vmNotes中的方法(NoteWindow的ViewModel)

  public void SaveChanges()
{
CurrentCalc.Save();
DC.SubmitChanges();
}

CurrentCalc是一个获取/设置我在数据绑定中使用的vmCalculation的属性(将DataGrid绑定到CurrentCalc.Notes)。



当我在CurrentCalc上运行AddNote()时,视图被更新为新注释-note。但是,当我运行SaveChanges()时,注释不会写入数据库。



有关此问题的任何想法?



问题的一个可能原因可能是,我不会在vmNotes中初始化DataContext(DC)。我从另一个ViewModel获取DataContext,以免我破坏MVVM结构。

解决方案

想到一个可能的解决方案我的问题。



我更新了vmNotes类上的SaveChanges() - 方法。

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

更新03/09/2011:



以上代码不需要。



我发现我有DataModel类的多个(和静态)实例。



我删除了其中的一些,现在我的原始代码工作正常!


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.

解决方案

Thought of a possible solution for my problem.

I updated the SaveChanges()-method on the vmNotes class a bit.

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

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!

这篇关于SubmitChanges不“提交”在LINQ to SQL中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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