1. Add model
What is the role model?
Data processing model projects, interact with the database
.net recommended way to deal with the data: using idd framework
1) New model
Right models folder, select Add, select the class, named after a good generate a class.
After adding some of its properties, for example as follows:
Movie Data Types
using System; using System.Data.Entity; namespace MvcMovie.Models { public class Movie { public int ID { get; set; } public string Title { get; set; } public DateTime ReleaseDate { get; set; } public string Genre { get; set; } public decimal Price { get; set; } } public class MovieDBContext : DbContext { public DbSet<Movie> Movies { get; set; } } }
2) VS comes with a free Microsoft's Local DB database
Open the web.config configuration file,
It can be seen that there connectString tag database connection string
To use the Movie Model therein will continue to add the appropriate connection string:
New tags connectionStrings: Example as follows:
<connectionStrings> <add name="MovieContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>
Right after the New Controller directory,
In the Scaffold new dialog box, select the MVC 5 with the Controller views, a using the Entity Framework , click Add.
fill in:
* Class model (Model class) select: Movie (MvcMovie.Models) .
· Context class data (Data context class) selection: MovieDBContext (MvcMovie.Models)
Tick tick all options were generated,
If the build fails, by: generating -> Rebuild "project name"
It is generated again.
The controller includes generating basic CRUD functions
Run the project, in the address bar to access / Movie, may see the following page:
We can operate in a relational database where.
Check the following location of the database,
App_Data directory swells into a mdf file for local db database file
While the Server Explorer also able to see the data connection here can be sql query and other operations