private void AddCurrentIndex(MongoDbIntKeyCount next) { var tableName = this.table; var bson = next.ToBsonDocument(); bson.Remove("_id"); bson.Remove("TableName"); this.repo.MongoDbIntKeyCountsCollection.UpdateOne <MongoDbIntKeyCount>(b => b.TableName == tableName, new BsonDocument { { "$set", bson } }); }
public T Add(T entity) { var thisType = typeof(T); if (thisType == typeof(MongoDbIntKeyCount)) { return(entity); } var tableName = this.table; Action action = () => { try { var isKeyLong = thisType.GetInterfaces().Any(i => i == typeof(IInt64Key)); MongoDbIntKeyCount currentLongIndex = null; if (isKeyLong) { currentLongIndex = CurrentIndexCount; ((IInt64Key)entity).Id = currentLongIndex.KeyCount + 1; } else { var key = thisType.GetProperties().Where(b => b.Name == "Id").FirstOrDefault(); if (key != null) { key.SetValue(entity, Guid.NewGuid().ToString()); } } var type = entity.GetType(); var ignoredKey = type.GetProperties().Where(b => b.CustomAttributes.Any(a => a.AttributeType.Name.Contains("NotMappedAttribute") || a.AttributeType.Name.Contains("IgnoreEdit")) ).Select(b => b.Name).ToList(); var bson = entity.ToBsonDocument <T>(); ignoredKey.ForEach(b => bson.Remove(b)); collectionBson.InsertOne(bson); if (currentLongIndex != null) { currentLongIndex.KeyCount = currentLongIndex.KeyCount + 1; AddCurrentIndex(currentLongIndex); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }; this.repo.ActionList.Add(action); return(entity); }