使用C#更新或插入mongodb集合中的项目列表

时间:2019-02-24 05:52:39

标签: c# mongodb mongodb-.net-driver

我有一个模型类,其中包括其他模型类的列表。此外,我使用通用存储库来管理数据库。模型类如下:

public abstract class Entity : Document
    {
        [BsonRepresentation(BsonType.Int32)]
        public int CreateUserId { get; set; }
        [BsonRepresentation(BsonType.Int32)]
        public int ChangeUserId { get; set; }
        [BsonDateTimeOptions(Kind = DateTimeKind.Utc, Representation = BsonType.DateTime)]
        public DateTime ChangeDate { get; set; }
        [BsonElement("CreateUser")]
        public virtual User CreateUser { get; set; }
        [BsonElement("ChangeUser")]
        public virtual User ChangeUser { get; set; }

    }

public class TrackedEntity : Entity
    {
        public bool Deleted { get; set; }

    }

public class GasStation : TrackedEntity
    {
        [BsonElement("Name")]
        public string Name { get; set; }
        public List<GasStationShift> GasStationShiftList { get; set; }
        public List<Tank> TankList { get; set; }
    }

public class GasStationShift : TrackedEntity
    {
        [BsonElement("ShiftId")]
        public int ShiftId { get; set; }
        [BsonRepresentation(BsonType.String)]
        public string GasStationId { get; set; }
        [BsonElement("StartFrom")]
        public string StartFrom { get; set; }
        [BsonElement("EndTo")]
        public string EndTo { get; set; }

    }

我的GenericRepository如下:

public class GenericRepository<T> : BaseMongoRepository, IRepository<T> where T : Entity
    {
        private readonly IMongoDbContext _context;
        public GenericRepository(IMongoDbContext context) : base(context)
        {

            _context = context;
        }
        public async Task AddAsync(T Item)
        {
            await _context.GetCollection<T>().InsertOneAsync(Item);
        }

        public async Task AddAllAsync(IEnumerable<T> Items)
        {
            await _context.GetCollection<T>().InsertManyAsync(Items);
        }

        public async Task DeleteAsync(Guid PrimaryKey)
        {
            var filter = Builders<T>.Filter.Eq("Id", PrimaryKey);
            await _context.GetCollection<T>().DeleteOneAsync(filter);
        }

        public async Task<List<T>> GetAsync(Expression<Func<T, bool>> predicate)
        {
            return await _context.GetCollection<T>().Find(predicate).ToListAsync();
        }

        public async Task<List<T>> GetAllAsync()
        {
            return await _context.GetCollection<T>().Find(_ => true).ToListAsync();
        }

        public async Task<T> GetByIDAsync(Guid Id)
        {
            var filter = Builders<T>.Filter.Eq("Id", Id);
            return await _context.GetCollection<T>().Find(filter).FirstOrDefaultAsync();
        }

        public async Task SaveAsync(T Item)
        {
            await _context.GetCollection<T>().InsertOneAsync(Item);
        }

        public async Task SaveAllAsync(IEnumerable<T> Items)
        {
            await _context.GetCollection<T>().InsertManyAsync(Items);
        }
    }

在我的服务层中,以更新GasStationShiftList或插入新项目,如下所示:

public async Task AddGasStationShift(GasStationShift gasStationShift)
        {
            GasStation gasStation = await _gasStationRepo.GetByIDAsync(Guid.Parse(gasStationShift.GasStationId));
            gasStation.GasStationShiftList.Add(gasStationShift);
// The type GasStation can not be used as type parameter TDocument compile time error
            await _gasStationRepo.UpdateOneAsync<GasStation>(gasStation);
        }

这是对集合的内部列表进行审核的正确方法吗?我该如何编写这段代码?

0 个答案:

没有答案