29.05.2015 Views

o_19mgorv9t13a3ko71fev19l81mqa.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Entity Framework should use to represent rows in that table. In this case, the property name is Products and the type<br />

parameter is Product, meaning that the Entity Framework should use the Product model type to represent rows in the<br />

Products table.<br />

Next, I need to tell the Entity Framework how to connect to the database, which I do by adding a database connection string to<br />

the Web.config file in the SportsStore.WebUI project with the same name as the context class, as shown in<br />

Listing 7-13.<br />

Listing 7-13. Adding a Database Connection in the Web.config File<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Tip Notice that I have switched project here. I define the model and the repository logic in the SportsStore.Domain<br />

project, but the database connection information is put in the Web.config file in the SportsStore.WebUI project.<br />

Caution I have had to split the value of the connectionString attribute across multiple lines to fit it on the page, but<br />

it is important to put everything on a single line in the Web.config file.<br />

There will be another add element in the connectionsStrings section of the Web.config file. Visual Studio<br />

creates this element by default and you can either ignore it or, as I have, delete it from the Web.config file.<br />

Creating the Product Repository<br />

All that remains is to add a class file to the Concrete folder of the SportsStore.Domain project called<br />

EFProductRepository.cs. Edit your class file so it matches Listing 7-14.<br />

Listing 7-14. The Contents of the EFProductRepostory.cs File<br />

using SportsStore.Domain.Abstract;<br />

using SportsStore.Domain.Entities;<br />

using System.Collections.Generic;<br />

namespace SportsStore.Domain.Concrete {<br />

public class EFProductRepository : IProductRepository {<br />

181

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!