/// <summary> /// 创建记录 /// </summary> /// <param name="entity">需要操作的实体类</param> /// <returns>操作是否成功</returns> public bool Create <T>(T entity) where T : ModelBase { // insert single data always return 0 but the data is inserted in database successfully //https://github.com/StackExchange/Dapper/issues/587 //List<T> data = new List<T>() { entity }; return(CurrentConnection.Insert(new List <T>() { entity }, CurrentTransaction) > 0); #region 测试代码 //string sql = @"INSERT INTO [dbo].[EFSample] // ([ID] // ,[CreateTime] // ,[ModifyTime] // ,[Available] // ,[UserName]) //VALUES // (@ID // ,@CreateTime // ,@ModifyTime // ,@Available // ,@UserName)"; //return CurrentConnection.Execute(sql, entity) > 0; #endregion 测试代码 }
public Task <long> InsertAsync(string tableName, string sbColumnList, string sbParameterList, object entityToInsert) { if (CurrentTransaction != null) { return(Task.FromResult(CurrentConnection.Insert(tableName, sbColumnList, sbParameterList, entityToInsert, CurrentTransaction, CommandTimeout))); } using var connection = GetDbConnection(DataSourceEnum.MASTER); return(Task.FromResult(connection.Insert(tableName, sbColumnList, sbParameterList, entityToInsert, CurrentTransaction, CommandTimeout))); }
/// <summary> /// 内部方法,sql不处理 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entityToInsert"></param> /// <returns></returns> public long Insert <T>(T entityToInsert) where T : class { if (CurrentTransaction != null) { return(CurrentConnection.Insert <T>(entityToInsert, CurrentTransaction, CommandTimeout)); } using var connection = GetDbConnection(DataSourceEnum.MASTER); return(connection.Insert <T>(entityToInsert, CurrentTransaction, CommandTimeout)); }