.netCore ABP 只修改部分字段 只更新部分列

.netCore ABP 只修改部分字段 只更新部分列_第1张图片
 
也可以弄一个通用的

using Abp.Dependency;

using Abp.EntityFrameworkCore;

using AutoCodePlan.EntityFrameworkCore;

using System.Collections.Generic;

using System.Threading.Tasks;

 

namespace AutoCodePlan.Sql

{

    ///

    /// 指定更新部分字段

    ///

    public interface ICustomModity

    {

        ///

        ///

        ///

        ///

        ///

        ///

        ///

        int UpdateEntityFields(T entity, IList fields) where T : class, new();

        ///

        ///

        ///

        ///

        ///

        ///

        ///

        Task UpdateEntityFieldsAsync(T entity, IList fields) where T : class, new();

    }

    ///

    ///

    ///

    public class CustomModity : ICustomModity, ITransientDependency

    {

 

        private IDbContextProvider _dbContextProvider = null;

        ///

        ///

        ///

        ///

        public CustomModity(IDbContextProvider dbContextProvider)

        {

            _dbContextProvider = dbContextProvider;//IocManager.Instance.Resolve>();

        }

 

 

        ///

        ///

        ///

        ///

        ///

        ///

        ///

        public int UpdateEntityFields(T entity, IList fields) where T : class, new()

        {

            if (entity == null || fields == null || fields.Count == 0)

            {

                return 0;

            }

            //db.Set().Attach(entity);

            var db = _dbContextProvider.GetDbContext();

            db.Set().Attach(entity);

            foreach (var item in fields)

            {

                db.Entry(entity).Property(item).IsModified = true;

            }

            return db.SaveChanges();

        }

 

 

        ///

        ///

        ///

        ///

        ///

        ///

        ///

        public async Task UpdateEntityFieldsAsync(T entity, IList fields) where T : class, new()

        {

            if (entity == null || fields == null || fields.Count == 0)

            {

                return 0;

            }

            //db.Set().Attach(entity);

            var db = _dbContextProvider.GetDbContext();

            db.Set().Attach(entity);

            foreach (var item in fields)

            {

                db.Entry(entity).Property(item).IsModified = true;

            }

            return await db.SaveChangesAsync();

        }

 

    }

}

你可能感兴趣的:(.net,Core,C#,c#,asp.net)