Exemple #1
0
        /// <summary>
        /// 保存数据库实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="tableName">表名称</param>
        /// <param name="dbModel">要保存的实体</param>
        /// <returns>保存成功返回实体对象;否则返回null</returns>
        public static BsonDocument SaveDbModelForNoType(string id, String tableName, BsonDocument dbModel)
        {
            //      获取主键信息;组件主键过滤的查询条件
            FilterDefinition <BsonDocument> filter = Builders <BsonDocument> .Filter.Eq(BaseField.Id, id);

            //      获取集合对象
            IMongoCollection <BsonDocument> collection = MongoDBHelper.GetMongoCollectionForNoType(tableName);
            //      判断数据对象是否存在,如果存在则更新,否则插入
            BsonDocument tmpDbModel = collection.FindOneAndReplace(filter, dbModel);

            if (tmpDbModel == null)
            {
                collection.InsertOne(dbModel);
            }
            tmpDbModel = collection.Find(filter).FirstOrDefault();
            //      返回保存后的数据
            return(tmpDbModel);
        }
Exemple #2
0
        /// <summary>
        /// 保存数据库实体
        /// </summary>
        /// <typeparam name="DbModel">关联的实体对象信息</typeparam>
        /// <param name="tableName">表名称</param>
        /// <param name="dbModel">要保存的实体</param>
        /// <returns>保存成功返回实体对象;否则返回null</returns>
        public static DbModel SaveDbModel <DbModel>(String tableName, DbModel dbModel) where DbModel : NoSqlBaseModel
        {
            //      获取主键信息;组件主键过滤的查询条件
            String id = Convert.ToString(dbModel.Id);
            FilterDefinition <DbModel> filter = Builders <DbModel> .Filter.Eq("_id", id);

            //      获取集合对象
            IMongoCollection <DbModel> collection = MongoDBHelper.GetMongoCollection <DbModel>(tableName);
            //      判断数据对象是否存在,如果存在则更新,否则插入
            DbModel tmpDbModel = collection.FindOneAndReplace(filter, dbModel);

            if (tmpDbModel == null)
            {
                collection.InsertOne(dbModel);
            }
            tmpDbModel = collection.Find(filter).FirstOrDefault();
            //      返回保存后的数据
            return(tmpDbModel);
        }