C#WPF MVVM实体框架代码需要第一个帮助!请! [英] C# WPF MVVM Entity Framework Code First Help Needed! Please!

查看:83
本文介绍了C#WPF MVVM实体框架代码需要第一个帮助!请!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家下午好,



基本上我正在创建一个存储黑胶唱片系列的程序。



该项目最初只是一个WPF应用程序,然后我不得不进行更改以使用MVVM模式实现它,然后替换使用SQL的旧学校方法,而不是使用EF。



但是,我可以使用EF获取我的数据并在列表视图中查看它。问题似乎是当我编辑一个条目时,我的更改发生在我点击更新时,但更改不会发送到SQL并保存。





为了澄清,我的项目需要采用MVVM模式,使用实体框架,代码首先处理sql数据,并且必须使用WPF进行c#。





非常感谢任何帮助或建议。到目前为止我没有回应,但很多人都看到这个。如果那里有人可以指出我正确的方向,我会非常感谢,谢谢。







以下是我的代码:







查看 - Xaml



Good afternoon everyone,

Basically I am creating a program to store a vinyl collection.

The project was originally just a WPF application, then I had to make the changes to implement it using MVVM pattern, and then replace the old school methods of using SQL, to use EF instead.

However, I can get my data and view it in a listview using EF. The problem seems to be when I edit an entry, my changes occur when I click update, but the changes are not sent to SQL and saved.


Just to clarify, my project needs to be in MVVM pattern, using entity framework, code first to deal with the sql data, and it has to be in c# using WPF.


Any help or advice would be greatly appreciated. I have had no response so far, but many people viewing this. If there is anyone out there that can point me in the right direction, I would really appreciate it, thanks.



Below is my code:



View - Xaml

<UserControl x:Class="VinylBox.View.ucVinyl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="485" d:DesignWidth="525">
    <Grid Margin="0,0,0,20">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <ListView Name="VinylGrid" Grid.Row="1" Margin="4,199,12,24" ItemsSource="{Binding Vinyls, Mode=TwoWay}" >
            <ListView.View>
                <GridView x:Name="grdVinyl">
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name, Mode=TwoWay}" Width="120"/>
                    <GridViewColumn Header="VinylLabel" DisplayMemberBinding="{Binding VinylLabel, Mode=TwoWay}" Width="70"/>
                    <GridViewColumn Header="Genre" DisplayMemberBinding="{Binding Genre, Mode=TwoWay}" Width="60"/>
                    <GridViewColumn Header="SubGenre" DisplayMemberBinding="{Binding SubGenre, Mode=TwoWay}" Width="60"/>
                    <GridViewColumn Header="Photo" DisplayMemberBinding="{Binding Photo, Mode=TwoWay}" Width="40"/>
                    <GridViewColumn Header="Url" DisplayMemberBinding="{Binding Url, Mode=TwoWay}" Width="200"/>
                </GridView>
            </ListView.View>
        </ListView>
        <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" 
                 Margin="80,7,0,0" Name="txtName" VerticalAlignment="Top" Width="178"
                 Text="{Binding ElementName=VinylGrid, Path=SelectedItem.Name, Mode=TwoWay}" />
        <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" 
                 Margin="80,35,0,0" Name="txtVinylLabel" VerticalAlignment="Top" Width="178"
                 Text="{Binding ElementName=VinylGrid, Path=SelectedItem.VinylLabel, Mode=TwoWay}" />
        <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" 
                 Margin="80,62,0,0" Name="txtGenre" VerticalAlignment="Top" Width="178"
                 Text="{Binding ElementName=VinylGrid, Path=SelectedItem.Genre, Mode=TwoWay}" />
        <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" 
                 Margin="80,90,0,0" Name="txtSubGenre" VerticalAlignment="Top" Width="178"
                 Text="{Binding ElementName=VinylGrid, Path=SelectedItem.SubGenre, Mode=TwoWay}" />
        <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" 
                 Margin="80,150,0,0" Name="txtUrl" VerticalAlignment="Top" Width="279"
                 Text="{Binding ElementName=VinylGrid, Path=SelectedItem.Url, Mode=TwoWay}" />
        <Label Content="Name" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="10,10,0,397" Name="lblName"/>
        <Label Content="VinylLabel" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="10,35,0,372" Name="lblVinylLabel"/>
        <Label Content="Genre" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="10,62,0,345" Name="lblGenre"/>
        <Label Content="SubGenre" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="10,90,0,317" Name="lblSubGenre"/>
        <Label Content="Photo" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="263,10,0,397" Name="lblPhoto"/>
        <Label Content="Url" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="10,150,0,257" Name="lblUrl"/>
        <Image Name="imgPhoto" Source="{Binding ElementName=VinylGrid, Path=SelectedItem.Photo, Mode=TwoWay}" HorizontalAlignment="Left" Height="110" 
                Margin="263,35,0,0" Grid.RowSpan="2" VerticalAlignment="Top" Width="242"/>
        <Button Content="Update" Grid.Row="1" HorizontalAlignment="Left" Margin="366,150,0,255" Name="btnUpdate" Width="141" Command="{Binding Path=UpdateCommand}" VerticalAlignment="Center" Height="30"/>
    </Grid>
</UserControl>





查看 - 代码背后





View - Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VinylBox;
using VinylBox.ViewModel;
using VinylBox.Model;
using System.Collections.ObjectModel;

namespace VinylBox.View
{
    /// <summary>
    /// Interaction logic for ucVinyl.xaml
    /// </summary>
    public partial class ucVinyl : UserControl
    {
        public ucVinyl()
        {
            InitializeComponent();
            VinylViewModel vinylViewModel = new VinylViewModel();
            this.DataContext = vinylViewModel;
        }
    }
}





型号 - 代码背后





Model - Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;

namespace VinylBox.Model
{
    public class Vinyl : INotifyPropertyChanged
    {
        private string name;
        private string vinylLabel;
        private string genre;
        private string subGenre;
        private byte[] photo;
        private string url;

        public int VinylId { get; set; }

        public string Name
        {
            get 
            { 
                return name; 
            }
            set
            { 
                name = value; OnPropertyChanged("Name");
            }
        }

        public string VinylLabel
        {
            get
            {
                return vinylLabel;
            }
            set
            {
                vinylLabel = value; OnPropertyChanged("VinylLabel");
            }
        }

        public string Genre
        {
            get
            {
                return genre;
            }
            set
            {
                genre = value; OnPropertyChanged("Genre");
            }
        }

        public string SubGenre
        {
            get
            {
                return subGenre;
            }
            set
            {
                subGenre = value; OnPropertyChanged("SubGenre");
            }
        }

        public byte[] Photo
        {
            get
            {
                return photo;
            }
            set
            {
                photo = value; OnPropertyChanged("Photo");
            }
        }

        public string Url
        {
            get
            {
                return url;
            }
            set
            {
                url = value; OnPropertyChanged("Url");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}





查看模型 - 代码背后





View Model - Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.ComponentModel;
using System.Data.Entity;
using VinylBox.Model;
using VinylBox;
using System.Collections.ObjectModel;




namespace VinylBox.ViewModel
{
    class VinylViewModel
    {
        private IList<Vinyl> VinylList;
        private byte[] photo;
        ObservableCollection<Vinyl> obVinyl = new ObservableCollection<Vinyl>();
        

        public VinylViewModel()
        {
            using (var db = new VinylContext())
            {

                // Display all vinyl from the database

                var query = from b in db.VinylSet

                            orderby b.VinylId

                            select b;


                foreach (var item in query)
                {
                    obVinyl.Add(item);
                    VinylList = obVinyl;
                }
            }
        }

        public IList<Vinyl>Vinyls
            {
                get { return VinylList; }
                set { VinylList = value; }
            }


        public void InsertVinyl(int vinylId, string name, string vinylLabel, string genre, string subGenre, string url)
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<VinylContext>());
            VinylContext contextVinyl = new VinylContext();
            Vinyl vinyl = new Vinyl();

            using (var vinylContext = new VinylContext())
            {

                var query = from b in vinylContext.VinylSet

                            orderby b.VinylId

                            select b;


                foreach (var item in query.Where(v => v.VinylId == vinylId))
                {
                    if (vinylId == item.VinylId)
                    {

                        vinyl.Name = item.Name;
                        vinyl.VinylLabel = item.VinylLabel;
                        vinyl.Genre = item.Genre;
                        vinyl.SubGenre = item.SubGenre;
                        vinyl.Photo = item.Photo;
                        vinyl.Url = item.Url;

                    }
                    else
                    {

                        vinyl.Name = name;
                        vinyl.VinylLabel = vinylLabel;
                        vinyl.Genre = genre;
                        vinyl.SubGenre = subGenre;
                        vinyl.Url = url;

                        contextVinyl.VinylSet.Add(vinyl);
                        contextVinyl.SaveChanges();

                    }

                }
            }
        }

        private ICommand mUpdater;
        public ICommand UpdateCommand
        {
            get
            {
                if (mUpdater == null)
                    mUpdater = new Updater();
                return mUpdater;
            }
            set
            {
                mUpdater = value;
            }
        }
        private class Updater : ICommand
        {
            #region ICommand Members

            public bool CanExecute(object parameter)
            {
                return true;
            }

            public event EventHandler CanExecuteChanged;

            public void Execute(object parameter)
            {
              
            }


            #endregion

        }
    }
}





数据背景 - 代码背后





Data Context - Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using VinylBox.Model;

namespace VinylBox.ViewModel
{
    public class VinylContext : DbContext
    {
        public DbSet<Vinyl> VinylSet { get; set; }
    }
}







如果有人可以,我会非常感激指向一个教程/示例,分享我想要实现的目标。或提供任何建议,将不胜感激。



亲切的问候



迪恩




I would really appreciate it if someone can point me to a tutorial/example that shares what I am trying to achieve. Or providing any kind of advice, would be appreciated.

Kind Regards

Dean

推荐答案

这篇关于C#WPF MVVM实体框架代码需要第一个帮助!请!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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