private static void autosetUpdate <TEntity>(TEntity entity, IUpdateBuilder builder) where TEntity : class { //有并发控制 if (entity != null && entity is IHasConcurrencyStamp concurrencyStampEntity && !string.IsNullOrEmpty(concurrencyStampEntity.ConcurrencyStamp)) { builder.Column("ConcurrencyStamp", Guid.NewGuid().ToString()); builder.Where("ConcurrencyStamp=@OldConcurrencyStamp", new SqlBuilderParameter("OldConcurrencyStamp", concurrencyStampEntity.ConcurrencyStamp)); } var type = typeof(TEntity); if (typeof(IHasModificationTime).IsAssignableFrom(type)) { builder.Column("LastModificationTime", (entity as IHasModificationTime)?.LastModificationTime ?? DateTime.Now); } else { return; } if (entity != null && type.GetInterfaces().Any(m => m.IsGenericType && m.GetGenericTypeDefinition() == typeof(IModificationAudited <>))) { builder.Column("LastModifierId", entity != null ? type.GetProperty("LastModifierId").GetValue(entity, null) : 0); builder.Column("LastModifierName", entity != null ? (type.GetProperty("LastModifierName").GetValue(entity, null) ?? string.Empty) : string.Empty); } }
protected override void UpdateAttribute(IUpdateBuilder <UserEntity> update, string name, object value) { switch (name) { case nameof(UserResource.ParatextId): if (value == null) { update.Unset(u => u.ParatextId); update.Unset(u => u.ParatextTokens); } break; case nameof(UserResource.Site): SiteOptions siteOptions = _siteOptions.Value; string siteKey = siteOptions.Id; if (value == null) { update.Unset(u => u.Sites[siteKey]); } else if (((Site)value).CurrentProjectId == null) { update.Unset(u => u.Sites[siteKey].CurrentProjectId); } else { update.Set(u => u.Sites[siteKey].CurrentProjectId, ((Site)value).CurrentProjectId); } break; default: base.UpdateAttribute(update, name, value); break; } }
/// <summary> /// 更新数据表 /// </summary> /// <param name="executeModel"></param> /// <returns></returns> public int Update(ExecuteModel executeModel) { using (var db = new DbContext()) { //生成操作时间 //executeModel.Entity.oper_time = db.Database().GetDateTime; //设置不立即设置为根据主键更新,后续根据和条件更新 IUpdateBuilder updateBuilder = db.Update(executeModel.Entity, false); if (executeModel.Columns != null && executeModel.Columns.Length > 0) {//如果更新有指定列,则指定列 updateBuilder.Column(executeModel.Columns); } //获取条件sql var whereSql = GetConitionsql(executeModel.Conditions); updateBuilder.Where(whereSql); executeModel.Conditions?.ForEach(p => {//先设置参数,组装SQL updateBuilder.Parameters(p.Key, p.Value); }); //执行更新 return(updateBuilder.Execute()); } }
protected override void UpdateAttribute(IUpdateBuilder <TranscriberTaskEntity> update, string name, object value) { switch (name) { case nameof(TranscriberTaskResource.Book): if (value == null) { update.Unset(u => u.Book); } else { update.Set(u => u.Book, (string)value); } break; case nameof(TranscriberTaskResource.Description): if (value == null) { update.Unset(u => u.Description); } else { update.Set(u => u.Description, (string)value); } break; default: base.UpdateAttribute(update, name, value); break; } }
public string Update <T>(IUpdateBuilder <T> updateBuilder) { var filterColumns = updateBuilder.GetFilterColumns(true).ToList(); var setColumns = updateBuilder.GetSetColumns().ToList(); var underscoreRequired = filterColumns.Select(fc => fc.ColumnName).Intersect(setColumns.Select(sc => sc.ColumnName)).Any(); return(string.Join(Environment.NewLine, $"update {BuildTableIdentifier(updateBuilder.TableMap)}", $"set {string.Join(", ", setColumns.Select(sc => BuildColumnEquals(sc, prependUnderscore: underscoreRequired)))}", BuildWhereClause(filterColumns))); }
public static IUpdateBuilder Column(this IUpdateBuilder builder, bool condition, Func <string> func) { builder = builder ?? throw new ArgumentNullException(nameof(builder)); func = func ?? throw new ArgumentNullException(nameof(func)); if (condition) { return(builder.Column(func.Invoke())); } return(builder); }
public static IUpdateBuilder Column(this IUpdateBuilder builder, string columnName, object value, DbType?dbType = null, ParameterDirection?direction = null, int?size = null) { builder = builder ?? throw new ArgumentNullException(nameof(builder)); if (string.IsNullOrEmpty(columnName)) { throw new ArgumentException($"{nameof(columnName)}不能为空", nameof(columnName)); } //if (value == null) // throw new ArgumentException($"{nameof(value)}不能为空", nameof(value)); builder.Column(new SqlBuilderParameter(columnName, value, dbType, direction, size)); return(builder); }
public bool Update(IUpdateBuilder <TThisEntity> update, IEnumerable <string> ids) { if (!_updateAllowed) { return(false); } string id = ids.Single(); if (id == null) { update.Unset(_getFieldExpr); } else { update.Set(_getFieldExpr, id); } return(true); }
protected virtual void UpdateAttribute(IUpdateBuilder <TEntity> update, string name, object value) { // by default, resource attribute names are the same as entity property names PropertyInfo propInfo = typeof(TEntity).GetProperty(name); LambdaExpression field = GetField(propInfo.PropertyType, name); if (value == null) { MethodInfo unsetMethod = update.GetType().GetMethod("Unset"); MethodInfo genericUnsetMethod = unsetMethod.MakeGenericMethod(propInfo.PropertyType); genericUnsetMethod.Invoke(update, new object[] { field }); } else { MethodInfo setMethod = update.GetType().GetMethod("Set"); MethodInfo genericSetMethod = setMethod.MakeGenericMethod(propInfo.PropertyType); genericSetMethod.Invoke(update, new object[] { field, value }); } }
public bool Update(T t) { using (var context = _Context()) { IUpdateBuilder <T> temp = context.Update <T>(tableName, t); foreach (var item in typeof(T).GetProperties()) { if (item.CustomAttributes.FirstOrDefault(c => c.AttributeType.Name == "PrimaryKey") != null) { temp.Where(item.Name, item.GetValue(t)); } else { temp = temp.Column(item.Name, item.GetValue(t)); } } return(temp.Execute() > 0); } }
public UpdateBuilder(IUpdateBuilder builder, DbContext db) { this._builder = builder ?? throw new ArgumentNullException(nameof(builder)); this._db = db ?? throw new ArgumentNullException(nameof(db)); }
public UpdateStatement() { UpdateBuilder = new UpdateBuilder(); }
public static int Exec(this IUpdateBuilder builder) { return(exec(builder)); }
protected abstract void VisitUpdate(IFragmentBuilder parent, IQueryGraphBuilder graph, IUpdateBuilder expression);
protected override void VisitUpdate(IFragmentBuilder parent, IQueryGraphBuilder graph, IUpdateBuilder expression) { this.Push(new SqlUpdateWriter(parent, graph, this.Database, this, this.Parameters)); this.Peek.Write(expression); this.Pop(); }
protected override void VisitUpdate(IFragmentBuilder parent, IQueryGraphBuilder graph, IUpdateBuilder expression) { //Nothing to do. }
public bool Update(IUpdateBuilder <TThisEntity> update, IEnumerable <string> ids) { return(false); }