コード例 #1
0
        public bool UpdateNoSql(IImmutableList <BookChangeInfo> booksToUpdate)
        {
            if (_noSqlContext == null || !booksToUpdate.Any())
            {
                return(false);
            }

            foreach (var bookToUpdate in booksToUpdate)
            {
                switch (bookToUpdate.State)
                {
                case EntityState.Deleted:
                {
                    var noSqlBook = _noSqlContext.Find <BookListNoSql>(bookToUpdate.BookId);
                    _noSqlContext.Remove(noSqlBook);
                    break;
                }

                case EntityState.Modified:
                {
                    //Note: You need to read the actual Cosmos entity because of the extra columns like id, _rid, etc.
                    //Version 3 might make attach work https://github.com/aspnet/EntityFrameworkCore/issues/13633
                    var noSqlBook = _noSqlContext.Find <BookListNoSql>(bookToUpdate.BookId);
                    var update    = _sqlContext.Set <Book>()
                                    .ProjectTo <BookListNoSql>(SqlToNoSqlMapper)
                                    .Single(x => x.BookId == bookToUpdate.BookId);
                    SqlToNoSqlMapper.CreateMapper().Map(update, noSqlBook);
                    break;
                }

                case EntityState.Added:
                    var newBook = _sqlContext.Set <Book>()
                                  .ProjectTo <BookListNoSql>(SqlToNoSqlMapper)
                                  .Single(x => x.BookId == bookToUpdate.BookId);
                    _noSqlContext.Add(newBook);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            return(true);
        }
コード例 #2
0
        public bool UpdateNoSql(IImmutableList <BookChangeInfo> booksToUpdate)
        {
            if (_noSqlContext == null || !booksToUpdate.Any())
            {
                return(false);
            }

            foreach (var bookToUpdate in booksToUpdate)
            {
                switch (bookToUpdate.State)
                {
                case EntityState.Deleted:
                {
                    var noSqlBook = _noSqlContext.Find <BookListNoSql>(bookToUpdate.BookId);
                    _noSqlContext.Remove(noSqlBook);
                    break;
                }

                case EntityState.Modified:
                {
                    var noSqlBook = _noSqlContext.Find <BookListNoSql>(bookToUpdate.BookId);
                    var update    = _sqlContext.Set <Book>()
                                    .ProjectTo <BookListNoSql>(SqlToNoSqlMapper)
                                    .Single(x => x.BookId == bookToUpdate.BookId);
                    SqlToNoSqlMapper.CreateMapper().Map(update, noSqlBook);
                    break;
                }

                case EntityState.Added:
                    var newBook = _sqlContext.Set <Book>()
                                  .ProjectTo <BookListNoSql>(SqlToNoSqlMapper)
                                  .Single(x => x.BookId == bookToUpdate.BookId);
                    _noSqlContext.Add(newBook);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            return(true);
        }