上一篇讲了EnitityFramework的包引用以及创建数据库和修改表结构的方法,这一篇主要讲一下对于数据库的操作。
首先,先添加界面内容,源码如下:
<Page
x:Class="TestEntityFramework.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestEntityFramework"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:data="using:TestEntityFramework.Models"
mc:Ignorable="d">
<Grid Background="Gray">
<RelativePanel Width="400" HorizontalAlignment="Center">
<StackPanel Name="EpEdits" Orientation="Vertical" Width="100" RelativePanel.AlignLeftWithPanel="True" RelativePanel.AlignRightWithPanel="True">
<TextBox Name="EpId" PlaceholderText="id"/>
<TextBox Name="EpName" PlaceholderText="姓名"/>
<TextBox Name="EpAge" PlaceholderText="年龄"/>
<TextBox Name="EpSex" PlaceholderText="性别"/>
</StackPanel>
<StackPanel Name="btnOper" RelativePanel.Below="EpEdits" Orientation="Horizontal" RelativePanel.AlignHorizontalCenterWith="EpEdits" Margin="20">
<Button Content="添加" Click="btn_AddClick"/>
<Button Content="更新" Click="btn_UpdateClick"/>
<Button Content="删除" Click="btn_DelClick"/>
</StackPanel>
<Grid Name="List" RelativePanel.Below="btnOper">
<ListView Name="lstEmployees">
<ListView.ItemTemplate>
<DataTemplate x:DataType="data:employees">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{x:Bind ID,Mode=OneWay}"/>
<TextBlock Grid.Column="1" Text="{x:Bind Name,Mode=OneWay}"/>
<TextBlock Grid.Column="2" Text="{x:Bind Sex,Mode=OneWay}"/>
<TextBlock Grid.Column="3" Text="{x:Bind Age,Mode=OneWay}"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</RelativePanel>
</Grid>
</Page>
界面效果如下
添加一条记录
/// <summary>
/// 向数据库中添加一条记录
/// </summary>
private void btn_AddClick(object sender, RoutedEventArgs e)
{
using (var db = new EfDbContext())
{
var newEmployees = new employees
{
ID = EpId.Text.Trim(),
Name = EpName.Text.Trim(),
Sex = EpAge.Text.Trim(),
Age = EpSex.Text.Trim()
};
EpId.Text = string.Empty;
EpName.Text = string.Empty;
EpAge.Text = string.Empty;
EpSex.Text = string.Empty;
try
{
db.Employees.Add(newEmployees);
db.SaveChanges();
}
catch (Exception err)
{
Debug.WriteLine("添加记录异常,错误:{0}",err.InnerException.Message);
}
lstEmployees.ItemsSource = db.Employees.ToList();
}
}
更新一条记录
/// <summary>
/// 更新记录
/// </summary>
private void btn_UpdateClick(object sender, RoutedEventArgs e)
{
using (var db = new EfDbContext())
{
var newEmployees = new employees
{
ID = EpId.Text.Trim(),
Name = EpName.Text.Trim(),
Sex = EpAge.Text.Trim(),
Age = EpSex.Text.Trim()
};
EpId.Text = string.Empty;
EpName.Text = string.Empty;
EpAge.Text = string.Empty;
EpSex.Text = string.Empty;
try
{
db.Employees.Update(newEmployees);
db.SaveChanges();
}
catch (Exception err)
{
Debug.WriteLine("更新记录异常,错误:{0}", err.InnerException.Message);
}
lstEmployees.ItemsSource = db.Employees.ToList();
}
}
这里需要注意的是更新和新增的方法基本类似,只有调用api的时候有些区别。但是在新增前一定要判断主键是否存在,如果存在只能使用更新操作,如果不存在则要使用新增操作,否则程序会报错。
删除记录
/// <summary>
/// 删除记录
/// </summary>
private async void btn_DelClick(object sender, RoutedEventArgs e)
{
using (var db = new EfDbContext())
{
try
{
var Red = db.Employees.Where(t => t.ID == "123").First();
if (Red != null)
{
db.Employees.Remove(Red);
db.SaveChanges();
lstEmployees.ItemsSource = db.Employees.ToList();
}
}
catch (Exception)
{
MessageDialog dlg = new MessageDialog("没有找到id为123的员工", "错误");
await dlg.ShowAsync();
}
}
}
判断条件使用LINQ的语法,这点需要注意的。
另外需要说明的是这个ORM框架把存储过程也封装进去了,如果需要批量操作可以使用响应的函数,例如新增多条记录可以使用AddRange方法,使用方法和Add类似,这里就不重复说明了。
怎么样,使用起来很简单吧。如有错误,欢迎各位大神指正!!!