Database Reference
In-Depth Information
Figure 8-10. A model of books in categories
To test your repository, do the following:
1.
Create an empty solution. Right-click the solution in the Solution Explorer, and select
Add New Project. Add a new Class Library project. Name this new project
BookRepository .
Create a new database. Call the database Test . We'll create and drop this database in the
unit tests, so make sure you create a new empty database.
2.
3.
Add the Topic and Category tables along with the relation corresponding to the model in
Figure 8-10 . Import these tables into a new model. Alternatively, you can use Model First
to create the model and then generate the database script to create the database.
4.
Add the code in Listing 8-18. This will create a BookRepository class that handles inserts
and queries against the model.
Listing 8-18. The BookRepository Class That Handles Inserts and Queries Against the Model
namespace BookRepository
{
public class BookRepository
{
private TestEntities _context;
public BookRepository(TestEntities context)
{
_context = context;
}
public void InsertBook(Book book)
{
_context.Books.Add(book);
}
public void InsertCategory(Category category)
{
_context.Categories.Add(category);
}
 
Search WWH ::




Custom Search