EntityFramework实现指定字段的通用赋值

1、背景

平常我们在建表的时候一般除了业务字段,还会加上如Create_Date 、Create_User 、Update_Date 、Update_User这样的通用字段 ;

public Nullable<System.DateTime> Create_Date { get; set; }
        public string Create_User { get; set; }
        public Nullable<System.DateTime> Update_Date { get; set; }
        public string Update_User { get; set; }

这样的公共字段,主要用于数据变更的核查,那么在创建或者修改的时候,

一般的赋值方式是这样的

Python编程大全分享Python技术文章,实用案例,热点资讯。你想了解的Python的那些事都在这里...... 当你的才华还撑不起你的野心的时候,那就安静下来学习吧!公众号该公众号已被封禁      BQoolCommon_Inner_Country_Timezone model = new BQoolCommon_Inner_Country_Timezone();
            model.Create_Date = DateTime.UtcNow;
            model.Create_User = "test";
            model.Update_Date = DateTime.UtcNow;
            model.Update_User = "test";

直接在对象实体里面直接赋值,但是这样就会造成很多的重复工作,说实在的写得多了会烦,也容易出错;

2、实现

那么有没有偷懒的办法呢,答案是肯定的。

下面我们看下如何在EntityFramework实现指定通用字段的赋值;

2.1首先是添加数据

public void Create(TEntity instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            else
            {
                _context.Set<TEntity>().Add(instance);
                SetCreate(instance);
                SaveChanges(instance);
            }
        }

通过反射方式,对指定的字段赋值

private void SetCreate(TEntity instance)
        {
            var attachedEntry = _context.Entry(instance);


            PropertyInfo getProperty(string propertyName)
            {
                return attachedEntry.Entity.GetType().GetProperty(propertyName);
            }


            //Create_User
            if (getProperty("Create_User") != null && string.IsNullOrEmpty(getProperty("Create_User").GetValue(attachedEntry.Entity) as string))
            {
                getProperty("Create_User").SetValue(attachedEntry.Entity, _user);
            }
            else if (getProperty("CreateUser") != null && string.IsNullOrEmpty(getProperty("CreateUser").GetValue(attachedEntry.Entity) as string))
            {
                getProperty("CreateUser").SetValue(attachedEntry.Entity, _user);
            }


            //Create_Date
            if (getProperty("Create_Date") != null)
            {
                DateTime? createDate = getProperty("Create_Date").GetValue(attachedEntry.Entity) as DateTime?;
                if (createDate == null || createDate == DateTime.MinValue)
                {
                    getProperty("Create_Date").SetValue(attachedEntry.Entity, GetDBTime());
                }
            }
            else if (getProperty("CreateDate") != null && getProperty("CreateDate").GetValue(attachedEntry.Entity) as DateTime? == null)
            {
                DateTime? createDate = getProperty("CreateDate").GetValue(attachedEntry.Entity) as DateTime?;
                if (createDate == null || createDate == DateTime.MinValue)
                {
                    getProperty("CreateDate").SetValue(attachedEntry.Entity, GetDBTime());
                }
            }
        }

2.2再者是更新操作

public void Update(TEntity instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            else
            {
                SetUpdate(instance);


                //_context.Entry(instance).State = EntityState.Modified;
                _context.Set<TEntity>().AddOrUpdate(instance);
                SaveChanges(instance);
            }
        }

如上方式

public void SetUpdate(TEntity instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            else
            {
                var attachedEntry = _context.Entry(instance);


                if (attachedEntry.Entity.GetType().GetProperty("Update_User") != null && !string.IsNullOrEmpty(_user))
                {
                    attachedEntry.Entity.GetType().GetProperty("Update_User").SetValue(attachedEntry.Entity, _user);
                }
                else if (attachedEntry.Entity.GetType().GetProperty("UpdateUser") != null && !string.IsNullOrEmpty(_user))
                {
                    attachedEntry.Entity.GetType().GetProperty("UpdateUser").SetValue(attachedEntry.Entity, _user);
                }


                if (attachedEntry.Entity.GetType().GetProperty("Update_Date") != null)
                {
                    attachedEntry.Entity.GetType().GetProperty("Update_Date").SetValue(attachedEntry.Entity, GetDBTime());
                }
                else if (attachedEntry.Entity.GetType().GetProperty("UpdateDate") != null)
                {
                    attachedEntry.Entity.GetType().GetProperty("UpdateDate").SetValue(attachedEntry.Entity, GetDBTime());
                }
            }
        }

最后大功告成,再也不用每次都赋值一次了。

技术群:添加小编微信并备注进群

小编微信:mm1552923   

公众号:dotNet编程大全    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值