So far, we have already prepared a real database by "simulating data" from the Controller to the view template. In this tutorial, how to use SQL ServerExpress (commonly known as SQL ce) is used as our database engine (SQL ce is free of charge) and is also applicable to the complete SQL Server.
Database Access and Entity Framework
We will use the Entity Framework (EF) Support to query and update databases in the ASP. net mvc 3 project. EF (Entity Framework) Is a flexible object relationship ing (ORM) data API,This allows developers to use object-oriented methods to query and update data in databases.
In this tutorial, we will use EF to create a database. So, now we createArtists model, and then put the artist's albumIn the artists model, add a new model file named:Artists. CS, as shown belowCode:
Public ClassArtist {Public IntArtistid {Get;Set;}Public StringName {Get;Set;}}
UpdateAlbum class, The following code:
Public Class Album { Public Int Albumid { Get ; Set ;} Public Int Genreid { Get ; Set ;} Public Int Artistid { Get ; Set ;} Public String Title { Get ; Set ;} Public Decimal Price { Get ; Set ;} Public String Albumarturl { Get ; Set ;} Public Genre genre { Get ; Set ;} Public Artist { Get ; Set ;}}
Next, update the following classes.
Using System. Collections. Generic; Public Class Genre { Public Int Genreid {Get ; Set ;} Public String Name { Get ; Set ;} Public String Description { Get ; Set ;} Public List <album> albums { Get ; Set ;}}
Add the app_data folder
We will save the SQL file in the app_data folder. app_data is a special directory in ASP.. Net has set secure access permissions for database access. In the "project" menu, select "add ASP. net folder "->" app_data ":
Configure the connection string in the web. config file
We will add several lines of code to the Web. config file so that EF knows how to connect to our database and double-click the Web. config file located in the root directory of the project.
<Configuration> Add a <connectionstrings> node to the node. The Code is as follows:
<Connectionstrings> <Add name ="Musicstoreentities"Connectionstring="Data Source = | datadirectory | mvcmusicstore. SDF"Providername="System. Data. sqlserverce.4.0"/> </Connectionstrings>
AddContextClass
Add a name in ModelsMusicstoreentities. CS class
This article is only translated here. Someone in the garden said that the champion has translated it before, so I don't want to translate it any more. I am not getting started with MVC, follow the tutorials on the official website to continue learning MVC 3. I have learned about MVC over the past few days. learning new things is very helpful, especially those I have never touched before. I have to continue to work on MVC. MVC is great!
ASP. net mvc music store translated by champion
A picture is provided for you: