コード例 #1
0
        public async Task UpdateAsync(Book book)
        {
            var bookEntity = new DAL.Entities.Book
            {
                ActualPrice     = book.ActualPrice,
                Description     = book.Description,
                Id              = book.Id,
                ImageUrl        = book.ImageUrl,
                OrgPrice        = book.OrgPrice,
                PromotionalText = book.PromotionalText,
                PublishedOn     = book.PublishedOn,
                Publisher       = book.Publisher,
                Title           = book.Title
            };

            await context.Set <DAL.Entities.BookAuthor>()
            .Where(ab => ab.BookId == book.Id)
            .Include(p => p.Author)
            .ForEachAsync(item => {
                if (item.Author.Name != "")
                {
                    return;
                }
            });

            context.Update <DAL.Entities.Book>(bookEntity);
            await context.SaveChangesAsync();
        }
コード例 #2
0
        public async Task <Guid> AddAsync(Book book)
        {
            var idBook = Guid.NewGuid();

            book.Id = idBook;

            var entity = new DAL.Entities.Book
            {
                Id              = idBook,
                ActualPrice     = book.ActualPrice,
                Description     = book.Description,
                ImageUrl        = book.ImageUrl,
                OrgPrice        = book.OrgPrice,
                PromotionalText = book.PromotionalText,
                PublishedOn     = book.PublishedOn,
                Publisher       = book.Publisher,
                Title           = book.Title
            };

            await context.AddAsync(entity);

            foreach (var author in book.Authors)
            {
                var _author = context.Set <DAL.Entities.Author>()
                              .FirstOrDefault(a => a.Name == author);
                if (_author == null)
                {
                    var idAuthor = Guid.NewGuid();

                    context.Add(
                        new DAL.Entities.Author
                    {
                        Id   = idAuthor,
                        Name = author
                    }
                        );

                    context.Add(
                        new DAL.Entities.BookAuthor
                    {
                        AuthorId = idAuthor,
                        BookId   = idBook
                    }
                        );
                }
                else
                {
                    context.Add(
                        new DAL.Entities.BookAuthor
                    {
                        AuthorId = _author.Id,
                        BookId   = idBook
                    }
                        );
                }
            }

            await context.SaveChangesAsync();

            return(idBook);
        }