ASP. NET MVC EF database preliminary water

Source: Internet
Author: User

First, create an MVC project, reference EntityFramework, and create a student class

Using system;using system.collections.generic;using system.linq;using system.web;namespace EFF. models{public    class Student    {public        int StudentID {get; set;}        public string Name {get; set;}        public int Age {get; set;}        public string Address {get; set;}        public int Sexsk {get; set;}}    }

After that, create a new DBContext.cs (this should be used for creating the database)

Using system.collections.generic;using system.data.entity;using system.linq;using system.web;using EFF. models;using system.data.entity.modelconfiguration.conventions;namespace eff{Public    class DBEFFContext:D Bcontext    {public        dbset<student> Students {get; set;}}            }

Of course, we need to connect to the database

On the Web. config, add the <configuration>

  <connectionStrings>    <add name= "Dbeffcontext" connectionstring= "Data source=.; Initial catalog=isee;integrated security=true "providername=" System.Data.SqlClient "/> </  Connectionstrings>

Connect to database, library name: ISEE (I look at Caesar)

Then I run to see what the next Caesar ...

Things are coming out, but ....

Modify Model:student time, run again, will error, no way, had to check the

The results found:

EF Code First Migrations Database migration (is this a link?) )

Look, although solved, but every time a run requires .... In the VS Tool->nuget Package Manager, the package management console runs inside ....

Install entityframework:pm> install-package entityframework generate configuration.cs,pm> Enable-Migrations- Enableautomaticmigrations (then remember to change automaticmigrationsenabled to true) after successful execution, add the class file in the Migrations folder 201309201556388_ Initialcreate.cs,pm> Add-migration Initialcreate This should be an update. pm> update-database-verbosepm> Add-Migration Addcity executes the Package Manager console statement again pm> Update-database-verbose

The data is still there, but always feel like this, every time a change to model, you have to do a set of (always feel a bit not applicable ....) Then go and look for it.

Thanks to several references that have been greatly provided:

MVC adds fields using EF Code First Data migration

http://blog.csdn.net/kangk1992/article/details/10107363

EF Code First Migrations Database migration:

Http://www.cnblogs.com/libingql/p/3330880.html

Code first mode development in MVC, Database Generation and update (EF6):

http://blog.csdn.net/chenguang79/article/details/50847550

ASP. NET MVC EF database preliminary water

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.