In the previous article, we have displayed the data to the view, but the data comes from our mock iproductrepository. Before we implement repository, we need to create an SQL Server database and add some data. We will use the EF framework to operate the SQL Server database. EF is A. Net ORM framework. The ORM framework allows us to operate the tables, columns, and rows of the database like the object instance. Just like using a Normal C # object, doing so requires a little knowledge of LINQ, which is not a mysterious and difficult thing, I believe that everyone can master and use LINQ in a short time. Click the View menu, open the server resource manager, and click Connect to database. In the connection dialog box, set the server name (localdb) \ v11.0. This is a special name, indicating that you want to use the features of the local database. A new feature added in vs2012 is that you can use the SQL Server kernel to create an administrator-free local database. For detailed instructions, see the relevant documentation. Now, make sure that you have selected the Windows Authentication login method and set the database name to sportsstore. For example, click OK. A confirmation dialog box is displayed. Click Yes to create a new database. We now only have one product table, right-click the table folder, choose add new table: Create new table can have two ways, here we use T-SQL, because it is more accurate, more convenient, the table creation statement is as follows: Create Table products (
[Productid] int not null Primary Key Identity, [name] nvarchar (100) not null, [description] nvarchar (500) not null, [category] nvarchar (50) not null, [price] decimal (16, 2) not null
) This table is slightly different from the product model class we previously defined. Now click the update button and you will see an updated summary as follows: Click the update database button. The table will be created. We need to add some data to the table.
Create Entity Framework Context The latest EF version contains a very good feature called code-first, which means that we can define some classes in our model and then generate databases from these classes. Here, I don't want to talk about the advantages and disadvantages of this technology. Instead, I will show you a variant based on this code-first. We will integrate our model class and an existing database. Now we will install the EF framework to our project first. Open nuget toolkit Management Program Install the EF framework. For example, create a folder named concrete in the sportsstore. Domain project and add a file named efdbcontext, Code Using sportsstore. domain. entities; using system. data. entity; namespace sportsstore. domain. concrete {public class efdbcontext: dbcontext {public dbset <product> Products {Get; Set ;}} to use the advantages of the code-first feature, we need to create a class, it is derived from system. data. entity. dbcontext. this class automatically defines an attribute for each table in the database and specifies the table name as the attribute name. Our current attribute name is products and the type parameter is product. we want the product model to be used to display rows in the products table. Now we want to tell Entity Framework how to connect to the database, so we need to add a connection string
Sportsstore. webui In the Web. config file of the project. <Connectionstrings> <Add name = "efdbcontext" connectionstring = "Data Source = (localdb) \ v11.0; initialcatalog = sportsstore; Integrated Security = true" providername = "system. data. sqlclient "/> </connectionstrings>
Create Product Repository
Now, we need to add a file to the concrete folder named efproductrepository to implement true data operations. Edit this file as follows: Using sportsstore. domain. abstract; using sportsstore. domain. Entities; using system. LINQ; namespace sportsstore. domain. Concrete {
Public class efproductrepository: iproductsrepository {
Private efdbcontext context = new efdbcontext ();
Public iqueryable Products {
Get {return context. Products ;}
}
}
} This repository class implements the iproductrepository interface and uses an efdbcontext instance to retrieve data from the database. You will see how EF works. Now we need to edit the ninjectcontrollerfactory class and replace the mock object with real data. Using system; using system. collections. generic; using system. LINQ; using system. web; using system. web. MVC; using system. web. routing; using sportsstore. domain. abstract; using sportsstore. domain. entities; using Moq; using ninject;
Using sportsstore. domain. concrete; Namespace sportsstore. webui. infrastructure {public class kernel: kernel {private ikernel ninjectkernel; Public kernel () {kernel = new standardkernel (); kernel () ;}protected override icontroller getcontrollerinstance (requestcontext, type controllertype) {return controllertype = NULL? Null: (icontroller) ninjectkernel. get (controllertype);} private void addbindings () {mock <iproductsrepository> mock = new mock <iproductsrepository> (); // mock. setup (M => M. products ). returns (new list <product >{// New Product {name = "football", price = 25}, // New Product {name = "surf board ", price = 179}, // New Product {name = "running shoes", price = 95 }//}. asqueryable (); // ninjectkernel. bind <iproductsrepository> (). toconstant (mock. object );
Ninjectkernel. Bind <iproductsrepository> (). To <efproductrepository> (); }}} Run your project and you will see the following picture: in the next article, we will add more products and apply the paging technology so that users can browse our products, you can also move from one page to another, and we will gradually add functions such as shopping cart to expand this project step by step. If you like it, please continue to follow my article!