MVC automatically generates database "Code-first mode"

Source: Internet
Author: User
Tags connectionstrings

Usually after we have written the entity, we configure the data context object, and after the connection string is changed in the configuration file.   It is not possible to build the database and generate the database automatically, there are two key steps: 1. Enable Migrations 2. Update-database after playing the second step, the database is automatically generated. ------------------------------------instance----------------------------------------------------------------// Entity: Using system;using system.collections.generic;using system.componentmodel.dataannotations;using System.Linq; Using System.Web; Namespace pagingandsortinginmvc.entities{Public class Employeemaster    {[Key]Public string ID {get; set;} [Required (errormessage= "Please enter employee Name")]Public string Name {get; set;} [Required (errormessage= "Please enter mobile number")]Public string PhoneNumber {get; set;} [Required (errormessage= "Please enter email")]Public string Email {get; set;} [Required (errormessage= "Please enter Salary")]Public decimal Salary {get; set;}}} ------------------------------------------------------------------------------------------------------------- Created data context class: Using Pagingandsortinginmvc.entities;using system;using system.collections.generic;using System.data.entity;using system.linq;using system.web; Namespace pagingandsortinginmvc.models{Public class Applicationdbcontext:dbcontext    {Public Applicationdbcontext (): Base ("name=connectionstring")        { } Public dbset<employeemaster> Employees {get; set;}}}-----------------------------------------------------------------------------------------------------the database connection string in the configuration file:<connectionStrings><add name= "ConnectionString" connectionstring= "server=.; Database=pageandsortdb;uid=sa;pwd=password_1 "providername=" System.Data.SqlClient "/></connectionStrings> ----------------------------------------------------------------------------------------------------------- ----------------------------------------------- after setting these, compile the project: just run:1.   Enable Migrations----------generate a class file "Configuration.cs", change automaticmigrationsenabled property to true  namespace pagingandsortinginmvc.migrations{    using system;     using System.Data.Entity;    using System.Data.Entity.Migrations;     using System.Linq;     internal sealed class  Configuration : dbmigrationsconfiguration<pagingandsortinginmvc.models.applicationdbcontext>     {        public configuration ()          {             AutomaticMigrationsEnabled = true;        }          protected override void seed ( pagingandsortinginmvc.models.applicationdbcontext&Nbsp;context)         {             //  this method will be called after migrating  to the latest version.              //  you can use the dbset<t>. AddOrUpdate ()  helper extension method              //  to avoid creating duplicate seed data.  e.g.            //             //    context. People.addorupdate (            //       p => p.fullname,      &nbSp;     //      new person { fullname  =  "Andrew peters"  },             //      new Person { FullName =  "brice  Lambson " },            //       new Person { FullName =  "Rowan miller"  }             //    );             //        }     }}   2. Update-database        ---after playing this, we have built our database in the database, data sheet    

MVC automatically generates database "Code-first mode"

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.