示例#1
0
        /// <summary>
        /// 添加模型,挂起添加
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        public void AddModelToContext <T>(T model)
        {
            var t = model.GetType();
            //设置模型值
            var prop = t.GetProperty("CreatorID");

            if (prop != null)
            {
                prop.SetValue(model, LoginUser.ID);
            }

            prop = t.GetProperty("CreationTime");
            if (prop != null)
            {
                prop.SetValue(model, DateTime.Now);
            }


            var tDB = db.GetType();
            //获取DbSet属性
            var modelSet = tDB.GetProperty(t.Name);
            //属性实例
            var modelSetInst = modelSet.GetValue(db);
            //获取Add方法
            var mAdd = modelSet.PropertyType.GetMethod("Add");

            //用实例调用Add
            mAdd.Invoke(modelSetInst, new object[] { model });
        }