Code-first example, codefirst

Source: Internet
Author: User

Code-first example, codefirst

First, create an empty database.

Add a database class in vs2013 and follow the prompts to continue the next step

 

Database Class Code after adding

Namespace mvctest. Models

{

Using System;

Using System. Data. Entity;

Using System. ComponentModel. DataAnnotations. Schema;

Using System. Linq;

 

Public partial class Model1: DbContext

{

Public DbSet <student> Students {get; set ;}

Public Model1 ()

: Base ("name = Model1 ")

{

}

 

 

Protected override void OnModelCreating (DbModelBuilder modelBuilder)

{

}

}

}

Each time you add an object class, you need to add the corresponding database code under this file, similar:

Public DbSet <student> Students {get; set ;}

 

Entity code:

Public class student

{

[Databasegeneratedattrition (DatabaseGeneratedOption. Identity)]

Public int id {get; set ;}

Public string name {get; set ;}

Public string descript {get; set ;}

}

 

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.