Create a database using Mvc5+entity Framework6 's code First mode and implement additions and deletions

Source: Internet
Author: User

The Vs2017+sqlserver database is used here

First, create the project and reference the DLL:

1. Create an MVC Project

2. Install EF6.1.3 with NuGet

Second, create model

In the Models folder, create the corresponding model file, the class name established here is the name of the table in the database.
In this case, the relationship between tables can be established, this time will be established course (course), Student (student), Stucourse (curriculum student Relationship) as a demo

[Table ("T_course")]//You can modify the automatically generated table name in the form of attribute, otherwise the courses complex form will be generated
public class Course
{

public int ID {get; set;}

[Required]
public string Course_name {get; set;}

public string Course_code {get; set;}

Public virtual icollection<stucourse> stucousers {get; set;}

}

public class Student
{

public int ID {get; set;}

[Required]
[MaxLength (10)]
public string Stu_name {get; set;}

public string Stu_pwd {get; set;}

public string Stu_sex {get; set;}

public int Stu_age {get; set;}

Public virtual icollection<stucourse> stucousers {get; set;}

}

public class Stucourse
{

public int ID {get; set;}

public int StudentID {get; set;}

public int CourseID {get; set;}

Public virtual Student Student {get; set;}

Public virtual Course Course {get; set;}

}

Create a new Dal folder to create the database Context

public class Efdbcontext:dbcontext
{
Public Efdbcontext (): Base ("Efdbcontext")//Database link in Web. config node name
{

}

Public virtual dbset<student> Students {get; set;}

Public virtual dbset<course> Courses {get; set;}

Public virtual dbset<stucourse> Stucouse {get; set;}

}

Iv. Configuring database connection Information

Add the connectionstrings node to the Webconfig file, fill in the database connection information, here is the SQL Server I installed in this machine, built in advance Database:codefirst

Note that the location of this node, can not be placed first, otherwise it will be an error.

V. Generate a database migration file and update the DB

1. Package Manager console, library Package Manager, tools

2. Run the command enable-migrations,

At this time, you will find in the terminal more than a folder called Migrations, which has a Configuration.cs file

3. Run command add-migration, this command will add a database migration file under the migrations file, the file content is the code of the table that creates or modifies the database

4. Run the command update-database or Update-database-force, execute the latest database migration file, update the table information in DB

After creating the table to modify the table is very simple, directly modify the model, and then execute Add-migration, will automatically generate a database migration file, in the execution of Update-database will update the table in the database;

There is also a way to turn on automatic migration, modify the Configuration under the Migrations folder, change the value of automaticmigrationsenabled to true, so that after modifying the model can be straight

Execute the update-database command to update the DB.

Six, the creation of control and the implementation of the addition and deletionof the function (EntityFramework scaffolding will automatically according to the model to help us generate additions and deletions and the view, but also comes with bootstrap style )

1. Add control and select the MVC5 controller that contains the view

2. Drop-down box select Create model class and database context class

Click to add the following pop-up box will start to automatically generate additions and deletions page and corresponding functions

You can see the Controller and Views folder to help us generate the following files:

3. Change the Routeconfig file, set the students index as the default startup item

4. The result of running the program is as follows, at this time adding or removing the function can be used

Create a database using Mvc5+entity Framework6 's code First mode and implement additions and deletions

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.