wpfmysql增删改查_WPFMVVM+EF增删改查简单⽰例(⼀)实现了那些功能,先看看效果图:
项⽬⼯程⽬录:
接下来开始具体的步骤:
第⼀步:在VS中新建⼯程
第⼆步:使⽤NuGet 安装EntityFramework
第三步:使⽤NuGet 安装EntityFramework.SqlSreverCompact
第四步:在Entities⽂件夹下添加StudentEntity类
public class StudentEntity
{
public int StudentId { get; set; }
public string StudentName { get; set; }
c语言数据类型说明保留字public int StudentAge { get; set; }
public int StudentSex { get; set; }
public string StudentAddress { get; set; }
}
mmap函数详解第五步:在Mappings⽂件夹下添加实体映射StudentEntityMapping类
public class StudentEntityMapping : EntityTypeConfiguration
{
public StudentEntityMapping()
{
this.HasKey(t => t.StudentId);
this.ToTable("Student");
this.Property(t =>
t.StudentId).HasColumnName("StudentId").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();
this.Property(t =>
t.StudentName).HasColumnName("StudentName").HasColumnType(SqlDbType.NVarChar.ToString()).HasMaxLength().IsRequired();
this.Property(t => t.StudentAge).HasColumnName("StudentAge").HasColumnType(SqlDbType.Int.ToString()).IsRequired();
linux安装yumthis.Property(t => t.StudentSex).HasColumnName("StudentSex").HasColumnType(SqlDbType.Int.ToString()).IsRequired();
this.Property(t =>
t.StudentAddress).HasColumnName("StudentAddress").HasColumnType(SqlDbType.NVarChar.ToString()).HasMaxLength().IsRequir
}
}
第六步:在Dal⽂件夹下添加StudentDataContext类
public class StudentDataContext : DbContext
{
public StudentDataContext()
: base("StudentDataContext")
{
}
public DbSet Students { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.AddFromAssembly(typeof(StudentDataContext).Assembly);
}
}
第七步:在AppConfig添加数据库的连接字符串
注意此处的路径必须为绝对路径
第⼋步:在NuGet包管理器中打开“程序包管理器控制台”
mysql菜鸟教程增删改查
第九步:⽣成Migrations⽂件并创建数据库
A,在打开的程序包管理器控制台中输⼊  enable-migrations 命令此时会⾃动在⼯程⽬录下创建⼀个Migrations⽂件夹和Configuration.cs
sqlyog在哪里编辑语句
B,打开Configuration.cs 修改 AutomaticMigrationsEnabled = true;
C,在打开的程序包管理器控制台中输⼊update-database
D,执⾏完成后就会在E:\Sample\DataBase\StudentDataContext.sdf下创建好数据库,将此数据库包含在项⽬中并将其包含在项⽬中,并将其设置为始终复制,编译项⽬,
并再次修改App.Config中的连接字符串。connectionString="Data Source=DataBase\StudentDataContext.sdf"
第⼗步:在Dal下添加⼀个⽤于操作数据的类StudentDal
public class StudentDal
{
StudentDataContext studentDataContext = new StudentDataContext();
public List GetAllStudent()
{
return studentDataContext.Students.ToList();
}
public void Insert(StudentEntity studentEntity)
{
studentDataContext.Students.Add(studentEntity);
studentDataContext.SaveChanges();
}
public void Delete(StudentEntity studentEntity)
{map遍历的几种方式
studentDataContext.Students.Remove(studentEntity);
studentDataContext.SaveChanges();
}
public void Update(StudentEntity studentEntity)
{
var id = studentDataContext.Students.Find(studentEntity.StudentId);
var entry = studentDataContext.Entry(id);
entry.CurrentValues.SetValues(studentEntity);
entry.Property(p => p.StudentId).IsModified = false;
studentDataContext.SaveChanges();
}
}
第⼗⼀步:在Views⽂件加下添加AddOrEditWindow.xaml和StudentControl.xaml StudentControl.xaml
xmlns="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/winfx/2006/xaml"
xmlns:mc="/markup-compatibility/2006" xmlns:d="schemas.microsoft/expression/blend/2008"
xmlns:viewModel="clr-namespace:Sample.ViewModels"
xmlns:convert="clr-namespace:Sample.Convert"
mc:Ignorable="d" >

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。