EF 数据库增删改查
一、准备工作
1、SQL数据库,建一个数据库,数据库名称为WareHouse。
2、VS端,新建控制台应用程序,添加EF,连接数据库,添加连接字符串。
二、操作数据库(CRUD)
public class EFCodeFirstDbContext : DbContext
{
public EFCodeFirstDbContext() : base("name=MyStrConn")
{
}
public DbSet<LocationInfo> LocationInfos { get; set; }
}
public class LocationInfo
{
[Key]
public int LocationId { get; set; }//库位号
public string Type { get; set; }//种类
public int Number { get; set; }//数量
public string Material { get; set; }//材质
public string RFIDNumber { get; set; }//料盘的RFID号
public LocationInfo(int locationId,string type,int number,string material,string rfidNumber)
{
this.LocationId = locationId;
this.Type = type;
this.Number = number;
this.Material = material;
this.RFIDNumber = rfidNumber;
}
//定义无参数的构造函数主要是因为在通过DbSet获取对象进行linq查询时会报错
//The class 'EFCodeFirstModels.Student' ha