ASP. NET MVC5 uses EF to automatically generate databases in reverse

Source: Internet
Author: User
Tags name database

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.