C # codefirst (EF framework) Code first creates a database

Source: Internet
Author: User
Tags connectionstrings


Namespace Webef.model
{
public class Modelcontext:dbcontext//Inherit DbContext from EF framework
{

Public Modelcontext (): Base ("Name= profile Name") {//Read config file

/* Configuration file formatting

<connectionStrings>
<add name= "Profile Name"
connectionstring= "Data source= (., localhost, remote IP); Initial catalog= database name; User id=sql account; password= password; "
Providername= "System.Data.SqlClient"/>
</connectionStrings>

*/
}

A class is a table

Public dbset< class name (table name) > class (table) name {set;get;} Storing objects in Dbset memory
Public dbset< class name (table name) > class (table) name {set; get;}
Public dbset< class name (table name) > class (table) name {set; get;}

protected override void Onmodelcreating (Dbmodelbuilder modelBuilder)
{
Base. Onmodelcreating (ModelBuilder);


modelbuilder.entity< Class name (table name) > (). ToTable ("table name");//alias to database table

modelbuilder.entity< Class name (table name) > (). ToTable ("table name");

modelbuilder.entity< Class name (table name) > (). Haskey (p = new {p.studentno, P.courseno}). ToTable ("table name");//p set two primary keys for an SCC
}
}
}

Dbfrist refers to database precedence, which maps a database into a solid model.

Modelfrist refers to pattern precedence, which maps solid modules to databases.

Codefrist is the use of C # code to generate a database.

Call

Suppose you create a student table and a course table and you need to create a student class and course class as follows


Namespace WEBEF
{
public partial class WebForm1:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
using (var context = new Modelcontext ())
{

Context. Database.createifnotexists ();


var student = new Student ("10001", "John Doe", "male", "CS");//Instantiate Student Class (student table, add a student information)

var c = New course//Instantiate course class, (curriculum, add a course)

{
No=1,
Name= "C Language",
Preno=null,
Credit=3

};



Context. Student.add (Student);//Add a student information

Context. Course.add (c);//Add a course
Context. SaveChanges ();//Save changes to save the data to the database

Response.Write ("Operation succeeded! ");
}

}
}
}

Student class


Namespace Webef.model
{
public class Student
{

private string No;
private string name;
private string sex;
private short age;
private String Dept;

Public Student () {}
Public Student (string No., String name, String sex, Short age, String dept)
{
this.no = no;
THIS.name = name;
This.sex = sex;
This.age = age;
this.dept = Dept;
}
<summary>
[key] Set Primary key
No indicates the number of studies
</summary>
[Key]
public string No {get {return this.no;} set {this.no = value;}}

<summary>
Name
</summary>
public string Name {get {return this.name;} Set{this.name=value;}}

<summary>
Gender
</summary>
public string Sex {set {this.sex = value;} get {return Sex;}}

<summary>
Age
</summary>
Public short Age {set {this.age = value;} get {return-age;}}

<summary>
Your department
</summary>
public string Dept {set {this.dept = value;} get {return Dept;}}

[ForeignKey ("Studentno")]//setting foreign keys
Public virtual list<scc> SCCs {set; get;}


}
}

Course Category


Namespace Webef.model
{
public class Course
{

<summary>
Course Number
</summary>
[Key]
public int No {set; get;}

<summary>
Course Name
</summary>
public string Name {set; get;}

<summary>
First class number
</summary>
///

public int? Preno {set; get;} The number indicates that the value can be empty

<summary>
Course Xuef
</summary>
Public short credits {set; get;}

[ForeignKey ("Courseno")]//set the course number as the foreign key for the selected schedule
Get all Elective courses
Public virtual list<scc> SCCs {set; get;}


[ForeignKey ("Preno")]//set the first course as a foreign key

Get all the first lessons
Public virtual list<course> Courses {set; get;} Get all the first lessons

}
}

Choose a timetable (a multi-to-many relationship between the student and the curriculum for the student table and the table of the curriculum)

Namespace Webef.model
{
public class SCC
{
<summary>
School Number
</summary>
///

public string Studentno {set; get;}

<summary>
Elective number
</summary>
///

public int Courseno {set; get;}


<summary>
Scores
</summary>
public int Grade {set; get;}


}
}

C # codefirst (EF framework) Code first creates a database

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.