码迷,mamicode.com
首页 > 其他好文 > 详细

Git.Framework 框架随手记--ORM新增操作

时间:2014-05-06 10:04:28      阅读:771      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   java   

  本篇主要记录具体如何新增数据,废话不多说,开始进入正文。

  一. 生成工程结构

    上一篇已经说到了如何生成工程结构,这里在累述一次。

    1. 新建项目总体结构

      使用VS新建项目结构,分层结构可以随意。我们使用的结构如下:

      bubuko.com,布布扣

    2. 引入配置文件相关

      Configs文件夹中的配置文件,其目录结构如下图:

      bubuko.com,布布扣

      以上几个文件为必须的,除了最下面的画红线的为自定义可以修改,具体配置项内容可以参考前面几篇文章。然后再web.config定义如下配置:

bubuko.com,布布扣
<appSettings>
    <add key="DatabaseListFile" value="/Configs/Data/Database.config"/>
    <add key="DataCommandFile" value="/Configs/Data/DbCommandFiles.config"/>
    <add key="console" value="true"/>
    <add key="file" value="true"/>
    <add key="level" value="info"/>
    <add key="logpath" value="\Log\"/>
    <add key="logtype" value="Daily"/>
  </appSettings>
bubuko.com,布布扣

    3. 生成相应的代码

bubuko.com,布布扣
[TableAttribute(DbName = "JooShowGit", Name = "Admin", PrimaryKeyName = "ID", IsInternal = false)]
    public partial class AdminEntity : BaseEntity
    {
        public AdminEntity()
        {
        }

        [DataMapping(ColumnName = "ID", DbType = DbType.Int32, Length = 4, CanNull = false, DefaultValue = null, PrimaryKey = true, AutoIncrement = true, IsMap = true)]
        public Int32 ID { get; set; }

        public AdminEntity IncludeID(bool flag)
        {
            if (flag && !this.ColumnList.Contains("ID"))
            {
                this.ColumnList.Add("ID");
            }
            return this;
        }

        [DataMapping(ColumnName = "UserName", DbType = DbType.String, Length = 20, CanNull = false, DefaultValue = null, PrimaryKey = false, AutoIncrement = false, IsMap = true)]
        public string UserName { get; set; }

        public AdminEntity IncludeUserName(bool flag)
        {
            if (flag && !this.ColumnList.Contains("UserName"))
            {
                this.ColumnList.Add("UserName");
            }
            return this;
        }

        [DataMapping(ColumnName = "PassWord", DbType = DbType.String, Length = 50, CanNull = false, DefaultValue = null, PrimaryKey = false, AutoIncrement = false, IsMap = true)]
        public string PassWord { get; set; }

        public AdminEntity IncludePassWord(bool flag)
        {
            if (flag && !this.ColumnList.Contains("PassWord"))
            {
                this.ColumnList.Add("PassWord");
            }
            return this;
        }

        [DataMapping(ColumnName = "UserCode", DbType = DbType.String, Length = 40, CanNull = false, DefaultValue = null, PrimaryKey = false, AutoIncrement = false, IsMap = true)]
        public string UserCode { get; set; }

        public AdminEntity IncludeUserCode(bool flag)
        {
            if (flag && !this.ColumnList.Contains("UserCode"))
            {
                this.ColumnList.Add("UserCode");
            }
            return this;
        }

        [DataMapping(ColumnName = "RealName", DbType = DbType.String, Length = 40, CanNull = false, DefaultValue = null, PrimaryKey = false, AutoIncrement = false, IsMap = true)]
        public string RealName { get; set; }

        public AdminEntity IncludeRealName(bool flag)
        {
            if (flag && !this.ColumnList.Contains("RealName"))
            {
                this.ColumnList.Add("RealName");
            }
            return this;
        }

        [DataMapping(ColumnName = "Email", DbType = DbType.String, Length = 30, CanNull = true, DefaultValue = null, PrimaryKey = false, AutoIncrement = false, IsMap = true)]
        public string Email { get; set; }

        public AdminEntity IncludeEmail(bool flag)
        {
            if (flag && !this.ColumnList.Contains("Email"))
            {
                this.ColumnList.Add("Email");
            }
            return this;
        }

        [DataMapping(ColumnName = "Mobile", DbType = DbType.String, Length = 11, CanNull = true, DefaultValue = null, PrimaryKey = false, AutoIncrement = false, IsMap = true)]
        public string Mobile { get; set; }

        public AdminEntity IncludeMobile(bool flag)
        {
            if (flag && !this.ColumnList.Contains("Mobile"))
            {
                this.ColumnList.Add("Mobile");
            }
            return this;
        }

        [DataMapping(ColumnName = "Phone", DbType = DbType.String, Length = 20, CanNull = true, DefaultValue = null, PrimaryKey = false, AutoIncrement = false, IsMap = true)]
        public string Phone { get; set; }

        public AdminEntity IncludePhone(bool flag)
        {
            if (flag && !this.ColumnList.Contains("Phone"))
            {
                this.ColumnList.Add("Phone");
            }
            return this;
        }

        [DataMapping(ColumnName = "CreateTime", DbType = DbType.DateTime, Length = 8, CanNull = false, DefaultValue = null, PrimaryKey = false, AutoIncrement = false, IsMap = true)]
        public DateTime CreateTime { get; set; }

        public AdminEntity IncludeCreateTime(bool flag)
        {
            if (flag && !this.ColumnList.Contains("CreateTime"))
            {
                this.ColumnList.Add("CreateTime");
            }
            return this;
        }

        [DataMapping(ColumnName = "CreateIp", DbType = DbType.String, Length = 20, CanNull = true, DefaultValue = null, PrimaryKey = false, AutoIncrement = false, IsMap = true)]
        public string CreateIp { get; set; }

        public AdminEntity IncludeCreateIp(bool flag)
        {
            if (flag && !this.ColumnList.Contains("CreateIp"))
            {
                this.ColumnList.Add("CreateIp");
            }
            return this;
        }

        [DataMapping(ColumnName = "CreateUser", DbType = DbType.String, Length = 30, CanNull = true, DefaultValue = null, PrimaryKey = false, AutoIncrement = false, IsMap = true)]
        public string CreateUser { get; set; }

        public AdminEntity IncludeCreateUser(bool flag)
        {
            if (flag && !this.ColumnList.Contains("CreateUser"))
            {
                this.ColumnList.Add("CreateUser");
            }
            return this;
        }

        [DataMapping(ColumnName = "LoginCount", DbType = DbType.Int32, Length = 4, CanNull = false, DefaultValue = null, PrimaryKey = false, AutoIncrement = false, IsMap = true)]
        public Int32 LoginCount { get; set; }

        public AdminEntity IncludeLoginCount(bool flag)
        {
            if (flag && !this.ColumnList.Contains("LoginCount"))
            {
                this.ColumnList.Add("LoginCount");
            }
            return this;
        }

        [DataMapping(ColumnName = "Picture", DbType = DbType.String, Length = 60, CanNull = true, DefaultValue = null, PrimaryKey = false, AutoIncrement = false, IsMap = true)]
        public string Picture { get; set; }

        public AdminEntity IncludePicture(bool flag)
        {
            if (flag && !this.ColumnList.Contains("Picture"))
            {
                this.ColumnList.Add("Picture");
            }
            return this;
        }

        [DataMapping(ColumnName = "UpdateTime", DbType = DbType.DateTime, Length = 8, CanNull = false, DefaultValue = null, PrimaryKey = false, AutoIncrement = false, IsMap = true)]
        public DateTime UpdateTime { get; set; }

        public AdminEntity IncludeUpdateTime(bool flag)
        {
            if (flag && !this.ColumnList.Contains("UpdateTime"))
            {
                this.ColumnList.Add("UpdateTime");
            }
            return this;
        }

        [DataMapping(ColumnName = "IsDelete", DbType = DbType.Int16, Length = 2, CanNull = false, DefaultValue = null, PrimaryKey = false, AutoIncrement = false, IsMap = true)]
        public Int16 IsDelete { get; set; }

        public AdminEntity IncludeIsDelete(bool flag)
        {
            if (flag && !this.ColumnList.Contains("IsDelete"))
            {
                this.ColumnList.Add("IsDelete");
            }
            return this;
        }

        [DataMapping(ColumnName = "Status", DbType = DbType.Int16, Length = 2, CanNull = false, DefaultValue = null, PrimaryKey = false, AutoIncrement = false, IsMap = true)]
        public Int16 Status { get; set; }

        public AdminEntity IncludeStatus(bool flag)
        {
            if (flag && !this.ColumnList.Contains("Status"))
            {
                this.ColumnList.Add("Status");
            }
            return this;
        }

        [DataMapping(ColumnName = "DepartNum", DbType = DbType.String, Length = 20, CanNull = true, DefaultValue = null, PrimaryKey = false, AutoIncrement = false, IsMap = true)]
        public string DepartNum { get; set; }

        public AdminEntity IncludeDepartNum(bool flag)
        {
            if (flag && !this.ColumnList.Contains("DepartNum"))
            {
                this.ColumnList.Add("DepartNum");
            }
            return this;
        }

        [DataMapping(ColumnName = "ParentCode", DbType = DbType.String, Length = 40, CanNull = false, DefaultValue = null, PrimaryKey = false, AutoIncrement = false, IsMap = true)]
        public string ParentCode { get; set; }

        public AdminEntity IncludeParentCode(bool flag)
        {
            if (flag && !this.ColumnList.Contains("ParentCode"))
            {
                this.ColumnList.Add("ParentCode");
            }
            return this;
        }

        [DataMapping(ColumnName = "RoleNum", DbType = DbType.String, Length = 20, CanNull = false, DefaultValue = null, PrimaryKey = false, AutoIncrement = false, IsMap = true)]
        public string RoleNum { get; set; }

        public AdminEntity IncludeRoleNum(bool flag)
        {
            if (flag && !this.ColumnList.Contains("RoleNum"))
            {
                this.ColumnList.Add("RoleNum");
            }
            return this;
        }

        [DataMapping(ColumnName = "Remark", DbType = DbType.String, Length = 40, CanNull = true, DefaultValue = null, PrimaryKey = false, AutoIncrement = false, IsMap = true)]
        public string Remark { get; set; }

        public AdminEntity IncludeRemark(bool flag)
        {
            if (flag && !this.ColumnList.Contains("Remark"))
            {
                this.ColumnList.Add("Remark");
            }
            return this;
        }

    }
实体类AdminEntity
bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Git.Framework.ORM;
using Git.Storage.Entity.Base;

namespace Git.Storage.IDataAccess.Base
{
    public partial interface IAdmin : IDbHelper<AdminEntity>
    {
    }
}
数据访问接口
bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Git.Framework.ORM;
using Git.Framework.MsSql;
using Git.Storage.Entity.Base;
using Git.Storage.IDataAccess.Base;

namespace Git.Storage.DataAccess.Base
{
    public partial class AdminDataAccess : DbHelper<AdminEntity>, IAdmin
    {
        public AdminDataAccess()
        {
        }

    }
}
数据访问实现

 

  二. 使用IncludeAll() 方法新增

    可能大家觉得IncludeAll()方法很奇怪,这里我们先看一个SQL语句

bubuko.com,布布扣
INSERT INTO [dbo].[OutStorage]([OrderNum],[OutType],[ProductType],[CusNum],[CusName],[Contact],[Phone],[Address],[ContractOrder],[Num],[Amount],[Weight],[SendDate],[Status],[IsDelete],[CreateTime],[CreateUser],[AuditUser],[AuditeTime],[PrintUser],[PrintTime],[Reason],[OperateType],[EquipmentNum],[EquipmentCode],[Remark]) VALUES(@OrderNum,@OutType,@ProductType,@CusNum,@CusName,@Contact,@Phone,@Address,@ContractOrder,@Num,@Amount,@Weight,@SendDate,@Status,@IsDelete,@CreateTime,@CreateUser,@AuditUser,@AuditeTime,@PrintUser,@PrintTime,@Reason,@OperateType,@EquipmentNum,@EquipmentCode,@Remark);
bubuko.com,布布扣

    SQL语句中有个INSERT INTO TABLE (ColName1,ColName2,ColName3,...) 插入语句包含了插入的字段。在前面的映射过程中我们讲到了每个字段都有一个标识属性用于对一个数据库中的哪个字段,IncludeAll() 方法就是用于包含类中的所有标识对应的数据库字段。

bubuko.com,布布扣
public int AddAdmin(AdminEntity entity)
{
            entity.IsDelete = (int)EIsDelete.NotDelete;
            entity.CreateTime = DateTime.Now;
            entity.ParentCode = "";
            entity.IncludeAll();
            int line = this.Admin.Add(entity);
            return line;
}
添加代码

    上面的代码中有一句就是恩提桶有.IncludeAll() 该方法就是用于包含这个类中所有的属性字段。

bubuko.com,布布扣
INSERT INTO [dbo].[Admin]([UserName],[PassWord],[UserCode],[RealName],[Email],[Mobile],[Phone],[CreateTime],[CreateIp],[CreateUser],[LoginCount],[Picture],[UpdateTime],[IsDelete],[Status],[DepartNum],[ParentCode],[RoleNum],[Remark]) VALUES(@UserName,@PassWord,@UserCode,@RealName,@Email,@Mobile,@Phone,@CreateTime,@CreateIp,@CreateUser,@LoginCount,@Picture,@UpdateTime,@IsDelete,@Status,@DepartNum,@ParentCode,@RoleNum,@Remark);
bubuko.com,布布扣

    上面的方法调用可以生成如上的SQL代码,这些字段都是数据库表中对应的字段,使用下面的截图程序测试效果如下:

bubuko.com,布布扣

bubuko.com,布布扣

最终通过调用Add方法,将数据插入到了数据库,我们通过SQL管理器查询可以看到插入的数据。

 

  三. Include 方法添加

    在系统中提供了如下几个Include方法的重载和扩展

bubuko.com,布布扣
public static T Include<T, TKey>(this T entity, Expression<Func<T, TKey>> keySelector) where T : BaseEntity;
public static T Include<T>(this T entity, string propertyName) where T : BaseEntity;
public static T Include<T>(this T entity, string propertyName, string alias) where T : BaseEntity;
bubuko.com,布布扣

     以上三个方法都是扩展方法,第一个主要是用于对Lambda表达式的支持

entity.Include(a => new { a.DepartName,a.DicColumn,a.DepartNum,a.UserCode,a.UserName,a.PassWord });

    使用Lambda表达式可以更加方便的操作类的属性,可以使用.操作。 如果将new{} 这个里面指定所有的字段就和IncludeAll()方法一样。

    下面的两个方法第一个用于直接包含字段名称,最后一个则是包含字段名称并且指定别人,这个和在查询的时候有作用,查找字段指定别名。

bubuko.com,布布扣
public int AddAdmin(AdminEntity entity)
{
            entity.IsDelete = (int)EIsDelete.NotDelete;
            entity.CreateTime = DateTime.Now;
            entity.ParentCode = "";
            //entity.IncludeAll();
            entity.Include(a => new { a.DepartNum,a.UserCode,a.UserName,a.PassWord });
            int line = this.Admin.Add(entity);
            return line;
}
修改代码

    下面看看生成的SQL语句如下:

INSERT INTO [dbo].[Admin]([DepartNum],[UserCode],[UserName],[PassWord]) VALUES(@DepartNum,@UserCode,@UserName,@PassWord);

    并没有包含所有的字段信息,通过对比应该可以明白Include方法的作用了。 但是这里注意include 方法至少要包含一个字段,否则程序异常。如果建有数据库约束的字段也要包含,比如不允许为NULL的,如果不包含默认做NULL处理,插入语句报错。

 

  四. 插入集合

    这里的插入集合要注意只能少量的插入,如果一次性几万是存在问题的。还是直接看代码

bubuko.com,布布扣
public override string Create(InStorageEntity entity, List<InStorDetailEntity> list)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                int line = 0;
                entity.OrderNum = entity.OrderNum.IsEmpty() ? (new TNumProivder()).GetSwiftNum(typeof(InStorageEntity), 5) : entity.OrderNum;
                entity.IncludeAll();

                if (!list.IsNullOrEmpty())
                {
                    list.ForEach(a =>
                    {
                        a.IncludeAll();
                        a.OrderNum = entity.OrderNum;
                    });
                    entity.Num = list.Sum(q => q.Num);
                    entity.Amount = list.Sum(a => a.Amount);
                    line = this.InStorage.Add(entity);
                    line += this.InStorDetail.Add(list);
                }
                ts.Complete();
                return line > 0 ? EnumHelper.GetEnumDesc<EReturnStatus>(EReturnStatus.Success) : string.Empty;
            }
        }
批量INSERT

    方法this.InStorDetail.Add(list); 参数是一个List<T> 集合,调用该方法可以讲多条数据插入到数据库,但是在插入之前集合中的项都必须调用Include()或者IncludeAll() 用于包含插入哪些字段。

 

  五. 新增方法汇总

int Add(List<T> list);
int Add(T entity);
int Add(List<T> list, bool isOpenTrans);
int Add(T entity, bool isOpenTrans);

    在系统框架中提供了以上四种新增的方法,下面两个方法都多了一个参数就是在插入的时候是否启用事务操作,true表示启用,默认不启用

 

Git.Framework 框架随手记--ORM新增操作,布布扣,bubuko.com

Git.Framework 框架随手记--ORM新增操作

标签:des   style   blog   class   code   java   

原文地址:http://www.cnblogs.com/qingyuan/p/3708553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!