本文共 4359 字,大约阅读时间需要 14 分钟。
DbContext用于管理数据连接与操作,代码中使用了Entity Framework来实现数据增删改查。 GasBottles类暴露了基本的CRUD操作,并通过DbContext获取数据上下文,执行数据库操作。
以下是详细说明:
GasBottles
类实现了IGasBottles
接口,内部通过DbContext
操作数据库。主要方法包括:
GetModel
根据ID查询特定记录,返回模型对象。Add
将模型对象插入数据库。Update
根据变化数据更新数据库记录。Delete
标记指定记录为已删除。Save
或新增或更新记录,返回新的ID。DbContext 配置了数据上下文,支持两种运行环境:Winfrom方式
和 Web方式
。 在访问数据库时,لاح $"{contextClassName}DataContext" 会被缓存,以提升性能。
具体实现如下:
public static class DbContext { private static string connectionstring = SqlHelper.SQLConnString; public static WLMQGasBottlesDataContext GetWLMQDataContext() { var context = HttpContext.Current.Items["WLMQGasBottlesDataContext"] as WLMQGasBottlesDataContext; if (context == null) { context = new WLMQGasBottlesDataContext(connectionstring); HttpContext.Current.Items["WLMQGasBottlesDataContext"] = context; } return context; } public static LGSCMSDataContext GetLGSCMSDataContext() { var context = HttpContext.Current.Items["LGSCMSDataContext"] as LGSCMSDataContext; if(context == null) { context = new LGSCMSDataContext(connectionstring); HttpContext.Current.Items["LGSCMSDataContext"] = context; } return context; }}
CopyProperties
作为辅助方法,用于将源对象的属性复制到目标对象中。这对于批量更新或部分属性修改非常有用。
private void CopyProperties(ref T Target, T Source) { foreach (PropertyInfo prop in Target.GetType().GetProperties()) { if (prop.CanWrite && prop.CanRead) { prop.SetValue(Target, prop.GetValue(Source, null), null); } }}
查询数据库中指定ID的记录。若存在,返回对应的模型对象;否则返回 null。
public Model.GasBottles GetModel(int gasBottlesID) { var db = DbContext.LGSCMSDataContext; try { var gs = db.GasBottles.FirstOrDefault(s => s.ID == gasBottlesID); if (gs != null) { Model.GasBottles gasBottlesInfo = gs.ConvertToEntity(); return gasBottlesInfo; } } catch (Exception ex) { throw ex; } return null; }
将模型对象转换为实体,插入数据库。
public bool Add(Model.GasBottles gasBottles) { try { var db = DbContext.LGSCMSDataContext; var gs = gasBottles.ConvertToEntity(); db.GasBottles.InsertOnSubmit(gs); db.SubmitChanges(); return true; } catch (Exception ex) { return false; throw ex; } }
更新指定ID的记录。通过CopyProperties
方法复制源对象的属性到目标对象中。
public bool Update(Model.GasBottles gasBottles) { var changedData = gasBottles.ConvertToEntity(); var db = DbContext.LGSCMSDataContext; try { var updateTarget = db.GasBottles.SingleOrDefault(i => i.ID == gasBottles.ID); if (updateTarget != null) { CopyProperties(ref updateTarget, changedData); db.SubmitChanges(); } return true; } catch (Exception ex) { return false; throw ex; } }
标记记录为已删除。
public bool Delete(int gasBottlesID) { try { var db = DbContext.LGSCMSDataContext; var gs = db.GasBottles.FirstOrDefault(s => s.ID == gasBottlesID); if (gs != null) { gs.DeleteFlag = 1; db.SubmitChanges(); } return true; } catch (Exception ex) { return false; throw ex; } }
用于新增或更新记录。 支持增删两种模式:新增(ID为 0)或更新(已有 ID)。
public bool Save(Model.GasBottles gasBottles, out int gasBottlesID) { var changedData = gasBottles.ConvertToEntity(); var db = DbContext.LGSCMSDataContext; try { var updateTarget = db.GasBottles.SingleOrDefault(i => i.ID == gasBottles.ID); if (updateTarget == null) { db.GasBottles.InsertOnSubmit(changedData); db.SubmitChanges(); gasBottlesID = changedData.ID.ToInt(); return true; } else { CopyProperties(ref updateTarget, changedData); db.SubmitChanges(); gasBottlesID = updateTarget.ID.ToInt(); return true; } } catch (Exception ex) { return false; throw ex; } }
本文展示了一个使用 Entity Framework 的数据层实现,包含了与数据库交互的各项操作方法。代码设计清晰,注重Type-Safe记忆 enjoyable阅读。
转载地址:http://zuwfk.baihongyu.com/