如何在表中添加记录,以及如何在相关的相关表中添加记录

时间:2019-06-05 14:04:07

标签: c# entity-framework .net-core

我正在使用带有“代码优先”方法的Entity Framework开发图书馆应用程序。我有一个Books表,我需要在其中添加一条记录,但是当我添加一条记录时,我也需要在相关表中添加一条记录,因为Books与Writers and Genres表相关(实际上,因为Book可以有很多表类型的图书与BookGenres表相关。

这是我将书添加到表格中的命令:

EfAddBookCommand

public class EfAddBookCommand : IAddBookCommand
{
    private readonly LibraryContext _context;

    public EfAddBookCommand(LibraryContext context)
    {
        _context = context;
    }

    public void Execute(BookDto request)
    {
        if (_context.Books.Any(b => b.Title == request.Title))
        {
            throw new Exception();
        }

        if(!(_context.Writers.Any(w => w.Id == request.WriterId)))
        {
            throw new Exception();
        }

        if (!(_context.Genres.Any(g => g.Id == request.GenreId)))
        {
            throw new Exception();
        }

        _context.Books.Add(new Domain.Book
        {
            Title = request.Title,
            Description = request.Title,
            AvailableCount = request.AvailableCount,
            Count = request.Count,
            WriterId = request.WriterId
        });


    }
}}

现在,当我将记录添加到Books表中时,我不确定添加到这些相关表中的最佳方法是什么。问题是,我需要知道这些记录的ID,例如,如果我将某些内容添加到Writer,则需要知道其ID,以便可以将该ID写入EfAddBookCommand的WriterId

其他相关类别: EfAddWriterCommand

    public class EfAddWriterCommand : IAddWriterCommand
{
    private readonly LibraryContext _context;

    public EfAddWriterCommand(LibraryContext context)
    {
        _context = context;
    }

    public void Execute(WriterDto request)
    {
        if(_context.Writers.Any(w => w.Name == request.Name))
        {
            throw new Exception();
        }

        _context.Writers.Add(new Domain.Writer
        {
            Name = request.Name
        });

        _context.SaveChanges();
    }
}

}

Domain.Writer

public class Writer : BaseEntity
{
    public string Name { get; set; }
    public ICollection<Book> Books { get; set; }
}

Domain.Book

public class Book : BaseEntity
{
    public string Title { get; set; }
    public int? WriterId { get; set; }
    public Writer Writer { get; set; }
    public string Description { get; set; }
    public int AvailableCount { get; set; } 
    public int Count { get; set; } 
    public ICollection<BookGenre> BookGenres { get; set; }
    public ICollection<BookReservation> BookReservations { get; set; }
}

WriterDto

   public class WriterDto
{
    public string Name { get; set; }
}

BookDto

    public class BookDto
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Writer { get; set; }
    public int? WriterId { get; set; }
    public int? GenreId { get; set; }
    public string Description { get; set; }
    public int AvailableCount { get; set; } //available for Reservation
    public int Count { get; set; } //all books regardless whether reserved or not
    public IEnumerable<string> BookGenres { get; set; }
    //public IEnumerable<string> BookReservations { get; set; }
}

BooksController

    [Route("api/[controller]")]
[ApiController]
public class BooksController : ControllerBase
{

    private IAddBookCommand _addCommand;

    public BooksController(IAddBookCommand addCommand)
    {

        _addCommand = addCommand;
    }

    // POST: api/Books
    [HttpPost]
    public IActionResult Post([FromBody] BookDto dto)
    {
        try
        {
            _addCommand.Execute(dto);
            return StatusCode(201);
        }
        catch (Exception)
        {
            return StatusCode(500);
        }
    }

}

此外,由于Book可以具有多种类型,因此我应该可以插入BookGenres。

0 个答案:

没有答案