简体   繁体   English

c# WPF MvvM 应用程序DataGrid 未更新到数据库

[英]c# WPF MvvM application DataGrid not updating to the database

I am working on a WPF application using MvvM Light.我正在使用 MvvM Light 开发 WPF 应用程序。 I have created a databinding for my DataGrid and when I edit something there it does not update to the database and I am not sure where I went wrong.我为我的 DataGrid 创建了一个数据绑定,当我在那里编辑一些东西时,它不会更新到数据库,我不确定我哪里出错了。

This is my View code:这是我的查看代码:

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:NinjaApp_V2.Views"
    xmlns:ViewModel="clr-namespace:NinjaApp_V2.ViewModel" xmlns:NinjaApp_V2="clr-namespace:NinjaApp_V2" x:Name="NinjaCRUDWindow" x:Class="NinjaApp_V2.Views.NinjaCRUD"
    mc:Ignorable="d"
    Title="NinjaCRUD" Height="300" Width="300">
<Window.Resources>
    <CollectionViewSource x:Key="ninjaViewSource" d:DesignSource="{d:DesignInstance {x:Type NinjaApp_V2:ninja}, CreateList=True}"/>
</Window.Resources>
<Window.DataContext>
    <Binding Path="AddNinja" Source="{StaticResource Locator}"/>
</Window.DataContext>
<Grid DataContext="{Binding Source={StaticResource Locator}}" Margin="0,10,3.6,0.4">
    <DataGrid x:Name="DataGridNinjas" ItemsSource="{Binding Ninjas.Ninjas}">
        <DataGrid.ItemBindingGroup>
            <BindingGroup/>
        </DataGrid.ItemBindingGroup>
    </DataGrid>
    <Button Command="{Binding SaveCommand}" CommandParameter="{Binding ElementName=AddWindow}" Margin="0,180,211,0">Save</Button>
</Grid>

The .cs of that view is completly empty as I have seen in multiple examples.正如我在多个示例中看到的那样,该视图的 .cs 完全是空的。

AddNinjaVM contains the SaveCommand and looks like this: AddNinjaVM 包含 SaveCommand,如下所示:

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using NinjaApp_V2.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace NinjaApp_V2.ViewModel
{
    public class AddNinjaVM : ViewModelBase
    {
        public NinjaVM Ninja { get; set; }

        public ICommand SaveCommand { get; set; }

        private NinjaListVM _ninjas;

        public AddNinjaVM(NinjaListVM ninjas)
        {
            _ninjas = ninjas;
            Ninja = new NinjaVM();
            SaveCommand = new RelayCommand<NinjaCRUD>(Save);
        }

        private void Save(NinjaCRUD obj)
        {
            using (var context = new NinjaApp_DatabaseEntities())
            {
                context.SaveChanges();
            }

            _ninjas.Ninjas.Add(Ninja);
            obj.Hide();
        }
    }
}

I have followed a working example from school for this.为此,我遵循了学校的一个工作示例。 This is a part I have been stuck on for the second day now and I just do not get why it is not working in my application.这是我第二天一直坚持的部分,我只是不明白为什么它在我的应用程序中不起作用。 Could someone perhaps help me out?有人可以帮我吗?

 private void Save(NinjaCRUD obj)
        {
            using (var context = new NinjaApp_DatabaseEntities())
            {
                **context.<DBtable>.Add(obj);**
                context.SaveChanges(); <---- This is where it is wrong.
            }

            _ninjas.Ninjas.Add(Ninja);  
            obj.Hide();
        }

You need to add the object to the context.Ninjas collection first and then call context.SaveChanges.您需要先将对象添加到 context.Ninjas 集合中,然后再调用 context.SaveChanges。

Entity Framework Concepts实体框架概念

In Entity Framework - assume DbContext (NinjaApp_DatabaseEntities in your code) to be the Database.在实体框架中 - 假设 DbContext(代码中的 NinjaApp_DatabaseEntities)是数据库。 All tables in the actual database will appear as List (Classes called Entities) in this DbContext.实际数据库中的所有表将在此 DbContext 中显示为列表(称为实体的类)。

when you have to add a record to a table, you create a row which must have a value for each column in that table and then its inserted.当您必须向表中添加记录时,您创建一个行,该行必须为该表中的每一列都有一个值,然后将其插入。 Similarily, in entity framework, when you have to add a new record to table, you got to create an object of the Entities Type.同样,在实体框架中,当您必须向表中添加新记录时,您必须创建一个实体类型的对象。

From you example & comments - I visualize that the Table name is "ninjas" - Hence you see context.ninjas从你的例子和评论 - 我想象表名是“忍者” - 因此你看到 context.ninjas

Next you want to add a record to this table "ninjas" - which means the object that you can add to it must also be of type of "ninjas" class.接下来,您要向该表“ninjas”添加一条记录——这意味着您可以添加到其中的对象也必须是“ninjas”类的类型。

Since you have the code, you must the Actual Type of Class representing the table and you must create a new object of it - assign values and then Add it to the context.ninjas.Add() and finally call context.SaveChanges();既然你有代码,你必须代表表的类的实际类型,你必须创建它的一个新对象 - 分配值,然后将它添加到 context.ninjas.Add() 并最后调用 context.SaveChanges();

Refer: http://www.entityframeworktutorial.net/EntityFramework4.3/add-entity-using-dbcontext.aspx参考: http : //www.entityframeworktutorial.net/EntityFramework4.3/add-entity-using-dbcontext.aspx

  //create DBContext object
        using (var dbCtx = new SchoolDBEntities())
        {
            //Add Student object into Students DBset
            dbCtx.Students.Add(newStudent);

            // call SaveChanges method to save student into database
            dbCtx.SaveChanges();
        }

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

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