/// <summary> /// 保存数据。 /// 默认的表名就是类名, 若T类型上有指定的表名,则以此为准 /// 默认的自增列为Id,若T类型上有指定的自增列, 则以此为准。 /// 若value中的标识列大于0, 则修改。 若小于等于0, 则新增。 /// 注:主键为int /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="value">类型值</param> /// <returns>新增则返回自增列, 修改则返回本身标示列值。</returns> public int Save <T>(T value) where T : class { ThrowExceptionUtil.ArgumentNotNull(value, "value"); string identityColumnName = OrmUtil.GetIdentityColumnName <T>(); string tableName = OrmUtil.GetTableName <T>(); EntityProxy <T> entityProxy = EntityProxyManager.Instance.GetEntityProxy <T>(); object identity = entityProxy.GetPropertyValue(value, identityColumnName); ThrowExceptionUtil.ArgumentConditionTrue(identity != null, string.Empty, "未指定主键列"); if ((int)identity <= 0) { return(Insert <T>(value)); } else { BeeDataAdapter dataAdapter = BeeDataAdapter.From <T>(value); dataAdapter.RemoveKey(identityColumnName); Update(tableName, dataAdapter, SqlCriteria.New.Equal(identityColumnName, identity)); return((int)identity); } }
/// <summary> /// Provided for common clone for the object. /// the object should be a class. /// </summary> /// <typeparam name="T">the type of the object</typeparam> /// <param name="value">the instance of the type.</param> /// <returns>the cloned instance.</returns> public static T CommonClone <T>(object value) where T : class { ThrowExceptionUtil.ArgumentNotNull(value, "value"); BeeDataAdapter dataAdapter = BeeDataAdapter.From(value); return(ConvertDataToObject <T>(dataAdapter)); }
public object Test() { var data = BeeDataAdapter.New.Add("test", 1).Add("test2", DateTime.Now).Add("test3", 0.5533d).Add("test4", Guid.NewGuid()).Add("test5", null); var data2 = BeeDataAdapter.New.Merge(data, true).Add("test6", data); var json = SerializeUtil.ToJson(data2); return(BeeDataAdapter.From(json)); }
public virtual PageResult Show(int id) { BeeDataAdapter dataAdapter = new BeeDataAdapter(); if (id >= 0) { using (DbSession dbSession = GetDbSession()) { T result = dbSession.Query <T>(SqlCriteria.New.Equal(OrmUtil.GetIdentityColumnName <T>(), id)).FirstOrDefault(); dataAdapter = BeeDataAdapter.From <T>(result); } } ViewData.Merge(dataAdapter, true); return(View("BeeAutoShow")); }
public PageResult ShowArticle(int id) { WeiXinArticle article = null; using (DbSession dbSession = new DbSession(WeiXinConstants.WeiXinConnString)) { article = dbSession.Query <WeiXinArticle>(SqlCriteria.New.Equal("id", id)).FirstOrDefault(); } if (article == null) { return(null); } else { ViewData.Merge(BeeDataAdapter.From(article), false); return(View("ShowArticle")); } }
/// <summary> /// 新增数据。 /// 默认的表名就是类名, 若T类型上有指定的表名,则以此为准 /// 默认的自增列为Id,若T类型上有指定的自增列, 则以此为准。 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="value">类型值</param> /// <param name="removeIdentity">是否移除标示列,该值若为false, 则Insert语句中将可能包含自增列。</param> /// <returns>插入返回的自增列</returns> public int Insert <T>(T value, bool removeIdentity) where T : class { ThrowExceptionUtil.ArgumentNotNull(value, "value"); Type typeInfo = typeof(T); EntityProxy <T> entityProxy = EntityProxyManager.Instance.GetEntityProxy <T>(); string tableName = OrmUtil.GetTableName <T>(); string identityColumnName = OrmUtil.GetIdentityColumnName <T>(); BeeDataAdapter dataAdapter = BeeDataAdapter.From <T>(value); int result = Insert(tableName, dataAdapter, removeIdentity); if (result != -1) { entityProxy.SetPropertyValue(value, identityColumnName, result); } return(result); }