private async Task Save()
        {
            foreach (var noteViewModel in this.Notes)
            {
                noteViewModel.Note.Description = noteViewModel.Description;
            }

            var removedIds = _token.Notes
                             .Where(note => note.Id != default && Notes.All(model => model.Note.Id != note.Id))
                             .Select(entity => entity.Id)
                             .ToList();
            var notesToRemove = _context.Set <NoteEntity>().Where(entity => removedIds.Contains(entity.Id));

            _context.Set <NoteEntity>().RemoveRange(notesToRemove);

            var added = this.Notes.Where(model => model.Note.Id == default).Select(model => model.Note);

            _context.Set <NoteEntity>().AddRange(added);

            var changed = this.Notes
                          .Where(model => model.Note.Id != default)
                          .Select(model => model.Note)
                          .ToDictionary(entity => entity.Id);
            var changedIds   = changed.Keys.ToList();
            var noteEntities = _context.Set <NoteEntity>().Where(entity => changedIds.Contains(entity.Id)).ToList();

            noteEntities.ForEach(entity => entity.Apply(changed[entity.Id]));
            await _context.SaveChangesAsync();

            _token.Deactivate();
        }
Ejemplo n.º 2
0
        public void AddTwoEntities_GetAll()
        {
            sqlConnection  = new SqlConnection("Server=(localdb)\\mssqllocaldb;Database=ProductsTest");
            context        = new LocalDbContext(sqlConnection);
            anotherContext = new LocalDbContext(sqlConnection);

            if (!context.Database.Exists())
            {
                context.Database.Create();
            }

            var products = context.Set <Product>();

            products.Add(new Product {
                Name = "Product #1", Price = 1.23m, Count = 8
            });
            products.Add(new Product {
                Name = "Product #2", Price = 4.56m, Count = 3
            });
            context.SaveChanges();

            var allProducts = anotherContext.Set <Product>().ToArray();

            Assert.IsNotNull(allProducts);
            Assert.AreEqual(2, allProducts.Length);
            Assert.AreEqual("Product #1", allProducts[0].Name);
            Assert.AreEqual(1.23m, allProducts[0].Price);
            Assert.AreEqual(8, allProducts[0].Count);
            Assert.AreEqual("Product #2", allProducts[1].Name);
            Assert.AreEqual(4.56m, allProducts[1].Price);
            Assert.AreEqual(3, allProducts[1].Count);
        }
Ejemplo n.º 3
0
 public void DeleteAll()
 {
     _dbContext.Set <T>().RemoveRange(GetAll());
 }
Ejemplo n.º 4
0
 public Repository(LocalDbContext _context)
 {
     this._context = _context;
     table         = _context.Set <T>();
 }
Ejemplo n.º 5
0
        public virtual IQueryable <T> GetIncluding(params Expression <Func <T, object> >[] properties)
        {
            IQueryable <T> query = _localDbContext.Set <T>();

            return(properties.Aggregate(query, (current, expression) => current.Include(expression)));
        }
Ejemplo n.º 6
0
 public BaseRepository(LocalDbContext context)
 {
     _context = context;
     dbSet    = _context.Set <T>();
 }