Имеется модель book,репозиторий и bdcontext. как через репозиторий работать с контроллером?
book:
public class book
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public string Author { get; set; }
public string description { get; set; }
public int count { get; set; }
public string PhotoPath { get; set; }
public Genre? genres { get; set; }
}
interface
interface IBookView<T> where T : class
{
IEnumerable<T> GetBooksList();
book objectBook(int bookid);
void Add(T item);
void delete(int id);
void update(T item);
void Save();
}
dbcontext:
public class BooksContext : DbContext
{
public DbSet<book> Books { get; set; }
//public BooksContext()
//{
// Database.EnsureCreated();
//}
public BooksContext(DbContextOptions<BooksContext> options)
: base(options)
{
Database.EnsureCreated();
}
}
repository
public class BookRepository : IBookView<book>
{
private readonly BooksContext db;
public BookRepository(BooksContext context)
{
db =context;
}
public IEnumerable<book> GetBooksList()
{
return db.Books;
}
public book objectBook(int bookid)
{
return db.Books.Find(bookid);
}
public void Add(book books)
{
db.Books.Add(books);
}
public void update(book books)
{
db.Entry(books).State = EntityState.Modified;
}
public void delete(int id)
{
book books = db.Books.Find(id);
if (books != null)
db.Books.Remove(books);
}
public void Save()
{
db.SaveChanges();
}
}