1. In the model class, write the corresponding attributes.
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSystem.Data.Entity;6 7 namespaceMvcmovie.models8 {9 Public classMovieTen { One Public intID {Get;Set; } A Public stringTitle {Get;Set; } - PublicDateTime ReleaseDate {Get;Set; } - Public stringGenre {Get;Set; } the Public decimalPrice {Get;Set; } - } - - Public classMoviedbcontext:dbcontext + { - PublicDbset<movie> Movies {Get;Set; } + } A}
Movie
2. In the configuration file, write:
1 <add name="moviedbcontext"2 connectionstring="Data Source= (LocalDB) \v11.0; attachdbfilename=| datadirectory|\movies.mdf;integrated security=true"3 providername=" System.Data.SqlClient "/>
configuration file, connection string
3. Add a controller and select the model that we just created as models (that is, create a strongly typed view)
4. At this point, a rebuild of the project will generate a database (Movie.mdf) in the App_Data.
Entity Framework Code First detected that the database connection string is provided pointed to a Movies
database that Didn ' t exist yet, so Code first created the database automatically. The EF code first detects that the database's connection string points to a movie database, but the database does not exist, so code first automatically creates the database for us.
5.You don ' t actually need to add the MovieDBContext
connection string. If you don ' t specify a connection string, the Entity Framework would create a LocalDB database in the users directory with the Fully qualified name of the Dbcontextclass (in this case MvcMovie.Models.MovieDBContext
). You can name the database anything and as long as it has the . MDF suffix. For example, we could name the database myfilms.mdf.
The idea is that you don't actually have to add my string above to the WEBCONIFG file because EF creates a full-path name database for us according to the physical path of the user project. If you add a connection string, EF will create the database for you, as you wrote it.
The 6.EF database created for us is:
As you can see, the database that EF created for us, the string field, is empty by default. The ID field is the primary key by default.
ASP. NET MVC5 uses EF to automatically generate databases in reverse