配置文件信息App.config:
数据库IP地址为192.168.2.186 ,数据库名为 Eleven-Six , 用户名 123456,密码654321
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="EfDbContext" connectionString=" Server = 192.168.2.186; Initial Catalog=Eleven-Six; User ID=123456; Password=654321; " providerName="System.Data.SqlClient"/> </connectionStrings> </configuration>
1. 新建类:
public class Cable { public int CableId { get; set; } public string CableName { get; set; } }
public class CableLine { public int Id { get; set; } public int CableId { get; set; } public int LineId { get; set; } public string XDescription { get; set; } public string YDescription { get; set; } public string CableLineDescription { get; set; } public double BreakOverUpper { get; set; } public double BreakOverLow { get; set; } }
2. 新建上下文:
public class EfDbContext:DbContext { public DbSet<Cable> Cables { get; set; } public DbSet<CableLine> CableLines { get; set; } }
2.1 新建 上下文简单工厂类:
public static class CableInformationOperateFactory { private static EfCableInformationOperate efCableInformationOperate = new EfCableInformationOperate(); public static ICableInformationOperate Get() { return efCableInformationOperate; } }
3. 新建 数据操作类EfCableInformationOperate,实现了接口 ICableInformationOperate
3.1 接口:
public interface ICableInformationOperate { void DeleteCable(int id); void DeleteCable(Cable cable); int AddCable(Cable cable, List<CableLine> cableLines); void UpdateCable(Cable cable); void UpdateCable(List<CableLine> cableLines); void UpdateCable(Cable cable, List<CableLine> cableLines); void UpdateCable(); List<Cable> GetCables(); Cable GetCable(int cableId); List<CableLine> GetCableLines(int cableId); }
3.2 实现类:
class EfCableInformationOperate:ICableInformationOperate { private EfDbContext _context = new EfDbContext(); public void DeleteCable(int id) { var cable = _context.Cables.Where(c => c.CableId == id); _context.Cables.RemoveRange(cable); var lines = _context.CableLines.Where(line => line.CableId == id); _context.CableLines.RemoveRange(lines); _context.SaveChanges(); } public void DeleteCable(Cable cable) { DeleteCable(cable.CableId); } public int AddCable(Cable cable, List<CableLine> cableLines) { _context.Cables.Add(cable); _context.SaveChanges(); int cableId = cable.CableId; cableLines.ForEach(line => line.CableId = cableId); _context.CableLines.AddRange(cableLines); _context.SaveChanges(); return cableId; } public void UpdateCable(Cable cable) { var oldinfor = _context.Cables.Find(cable.CableId); if (oldinfor != null) { oldinfor.CableName = cable.CableName; } _context.SaveChanges(); } public void UpdateCable(List<CableLine> cableLines) { throw new NotImplementedException(); } public void UpdateCable(Cable cable, List<CableLine> cableLines) { throw new NotImplementedException(); } public void UpdateCable() { _context.SaveChanges(); } public List<Cable> GetCables() { return _context.Cables.ToList(); } public Cable GetCable(int cableId) { return _context.Cables.Find(cableId); } public List<CableLine> GetCableLines(int cableId) { return _context.CableLines.Where(line => line.CableId == cableId).ToList(); } }
4. 测试类:
public class UnitTest1 {/// <summary> /// 添加数据条 测试 /// </summary> [TestMethod] public void AddTest() { List<CableLine> lines = new List<CableLine>(); for (int i = 0; i < 10; i++) { lines.Add(new CableLine() { BreakOverLow = i, BreakOverUpper = i + 10, CableLineDescription = i + "cable description", LineId = i, XDescription = i + "xdescription", YDescription = i + "ydescription" }); } CableInformationOperateFactory.Get().AddCable(new Cable() { CableName = "名字3" }, lines); } [TestMethod] public void DeleteTest() { CableInformationOperateFactory.Get().DeleteCable(1); } [TestMethod] public void UpdateTest() { CableInformationOperateFactory.Get().UpdateCable(new Cable() { CableId = 3, CableName = "新名字2" }); } [TestMethod] public void GetTest() { var operate = CableInformationOperateFactory.Get(); foreach (Cable cable in operate.GetCables()) { cable.CableName = "qweqw"; operate.UpdateCable(); } //foreach (CableLine line in CableInformationOperateFactory.Get().GetCableLines(5)) //{ // Console.WriteLine(line.CableLineDescription); //} } } }