博客
关于我
linq to sql 三层架构中使用CRUD操作
阅读量:800 次
发布时间:2023-01-31

本文共 4359 字,大约阅读时间需要 14 分钟。

DbContext用于管理数据连接与操作,代码中使用了Entity Framework来实现数据增删改查。 GasBottles类暴露了基本的CRUD操作,并通过DbContext获取数据上下文,执行数据库操作。

以下是详细说明:


Class Structure

GasBottles 类实现了IGasBottles 接口,内部通过DbContext 操作数据库。主要方法包括:

  • GetModel

    根据ID查询特定记录,返回模型对象。

  • Add

    将模型对象插入数据库。

  • Update

    根据变化数据更新数据库记录。

  • Delete

    标记指定记录为已删除。

  • Save

    或新增或更新记录,返回新的ID。


  • Configuration

    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;     }}

    Helper Method

    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); } }}

    Implementation Details

    GetModel Method

    查询数据库中指定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; }

    Add Method

    将模型对象转换为实体,插入数据库。

    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;     } }

    Update Method

    更新指定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;     } }

    Delete Method

    标记记录为已删除。

    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;     } }

    Save Method

    用于新增或更新记录。 支持增删两种模式:新增(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/

    你可能感兴趣的文章
    mysql8的安装与卸载
    查看>>
    MySQL8,体验不一样的安装方式!
    查看>>
    MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
    查看>>
    Mysql: 对换(替换)两条记录的同一个字段值
    查看>>
    mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
    查看>>
    MYSQL:基础——3N范式的表结构设计
    查看>>
    MYSQL:基础——触发器
    查看>>
    Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
    查看>>
    mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
    查看>>
    mysqldump 参数--lock-tables浅析
    查看>>
    mysqldump 导出中文乱码
    查看>>
    mysqldump 导出数据库中每张表的前n条
    查看>>
    mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
    查看>>
    Mysqldump参数大全(参数来源于mysql5.5.19源码)
    查看>>
    mysqldump备份时忽略某些表
    查看>>
    mysqldump实现数据备份及灾难恢复
    查看>>
    mysqldump数据库备份无法进行操作只能查询 --single-transaction
    查看>>
    mysqldump的一些用法
    查看>>
    mysqli
    查看>>
    MySQLIntegrityConstraintViolationException异常处理
    查看>>