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 ;}
}