ASP.Net核心Web Api與EF核心更新實體howto

asp.net-core asp.net-core-mvc entity-framework-core

不是重複 :我的Update()方法有兩個爭論:id和修改後的實體,提供給我的問題的答案有不同的方法,如CurrentValues ,現有問題中沒有涉及。

我是ASP.Net核心開發的新手。我正在使用MySql作為數據庫和Book模型構建一個簡單的API。

我寫了我的控制器如下:

[Route("api/[controller]")]
    public class BooksController : Controller
    {
        private readonly IBookRepository _books;
        public BooksController(IBookRepository books)
        {
            _books = books;
        }

        [HttpGet("")]
        public IActionResult GetAllBooks()
        {
            try
            {
                List<Book> books = _books.GetAllBooks();
                return Ok(books);
            }
            catch(EntityNotFoundException<Book>)
            {
                return NotFound();
            }

        }

        [HttpGet("{id}")]
        public IActionResult GetBook(long id)
        {
            Book book = _books.GetBook(id);
            if (book == null)
            {
                return NotFound();
            }
            return Ok(book);
        }

        [HttpPost]
        public IActionResult CreateBook([FromBody] Book book)
        {
            if (ModelState.IsValid == false)
            {
                return BadRequest(ModelState);
            }

            Book createdBook = _books.CreateBook(book);
            if (createdBook == null)
            {
                return NotFound();
            }
            return CreatedAtAction(
                 nameof(GetBook), new { id = createdBook.Id }, createdBook);

        }

        [HttpPut("{id}")]
        public IActionResult UpdateBook(long id, [FromBody] Book book)
        {
            if (ModelState.IsValid == false)
            {
                return BadRequest(ModelState);
            }

            try
            {
                _books.UpdateBook(id, book);
                return Ok();
            }
            catch (EntityNotFoundException<Book>)
            {
                return NotFound();
            }
        }

        [HttpDelete("{id}")]
        public IActionResult DeleteBook(long id)
        {
            _books.DeleteBook(id);
            return Ok();
        }
    } 

和存儲庫如下:

public class BookRepository: IBookRepository
    {
        private readonly WebAPIDataContext _db;

        public BookRepository(WebAPIDataContext db) {
            _db = db;
        }

        public Book CreateBook(Book book) {
            _db.Books.Add(book);
            _db.SaveChanges();
            return book;
        }

        public void DeleteBook(long id)
        {
            Book book = GetBook(id);
            if (book != null) {
                _db.Books.Remove(book);
                _db.SaveChanges();
            }
        }

        public List<Book> GetAllBooks()
        {
            return _db.Books.AsNoTracking().ToList();
        }

        public Book GetBook(long id)
        {
            return _db.Books.FirstOrDefault(o => o.Id == id);
        }

        public void UpdateBook(long id, Book book)
        {


        }
    }

    public interface IBookRepository
    {
        List<Book> GetAllBooks();
        Book GetBook(long id);
        Book CreateBook(Book book);
        void UpdateBook(long id, Book book);
        void DeleteBook(long id);
    }

我不知道如何在Repo類中編寫UpdateBook方法。有人可以幫助我嗎?我看到的所有教程都使用了單個id參數到UpdateBook()方法,我基本上發布了一個json對像以及實體id。

熱門答案

嘗試這個

  public void UpdateBook(long id, Book book)
  {  
     var originalBook=  _db.Books.FirstOrDefault(o => o.Id == id);
     _db.Entry(originalBook).CurrentValues.SetValues(book);
     _db.SaveChanges();
  }

請注意,您必須使用EFcore 1.1版才能工作



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow