The first step to create a new database and table
Use [TestDB]GO/** * * * object:table [dbo]. [T_user] Script date:01/14/2015 20:27:52 * * * * **/SETAnsi_nulls onGOSETQuoted_identifier onGOCREATE TABLE [dbo].[T_user]( [Id] [int] IDENTITY(1,1) not NULL, [Name] [nvarchar]( -)NULL, [Password] [nchar](Ten)NULL, CONSTRAINT [Pk_t_user] PRIMARY KEY CLUSTERED ( [Id] ASC) with(Pad_index= OFF, Statistics_norecompute= OFF, Ignore_dup_key= OFF, Allow_row_locks= on, Allow_page_locks= on) on [PRIMARY]) on [PRIMARY]GO
The second step is ADO. NET Entity Data Model new ADO Entity Data Model
The program automatically generates the following files
Testmodel.edmx
TestModel.Context.tt
TestModel.Context.tt is a template that generates data manipulation entity classes
namespaceefdemo1{usingSystem; usingSystem.Data.Entity; usingSystem.Data.Entity.Infrastructure; Public Partial classTestdbentities:dbcontext { Publictestdbentities ():Base("name=testdbentities")//Testdbentities as a database connection string in app. Config { } protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) {Throw Newunintentionalcodefirstexception (); } PublicDbset<t_user> T_user {Get;Set; } }}
TestModel.Designer.cs
TestModel.edmx.diagram
Testmodel.tt
Testmodel.tt templates are templates that generate T_user entity classes
namespaceefdemo1{usingSystem; usingSystem.Collections.Generic; Public Partial classT_user { Public intId {Get;Set; } Public stringName {Get;Set; } Public stringPassword {Get;Set; } }}
Modify Database
When modifying a database object (such as a table), you just need to right-click on the TESTMODEL.EDMX interface to update the model from the database, and save after the update is complete to automatically generate related code (such as entity classes).
The third step to delete and change
usingSystem;usingSystem.Data;usingSystem.Linq;usingSystem.Windows.Forms;namespaceefdemo1{ Public Partial classform1:form {testdbentities entity=Newtestdbentities (); PublicForm1 () {InitializeComponent (); } /// <summary> ///New/// </summary> Private voidAdd () {T_user model=NewT_user () {Name= DateTime.Now.ToString ("YYYY-MM-DD Hh:mm:ss") }; Entity. T_user.add (model); Entity. SaveChanges (); Query (); } /// <summary> ///Delete/// </summary> Private voidDelete () {if(Listbox1.selecteditem = =NULL) { return; } intid = Convert.ToInt32 (listBox1.SelectedItem.ToString (). Split ('-')[0]); T_user Model= entity. T_user.where (A = a.ID = =ID). FirstOrDefault (); if(Model! =NULL) {entity. Entry (model). State=entitystate.deleted; Entity. SaveChanges (); Query (); } } /// <summary> ///Modify/// </summary> Private voidEdit () {if(Listbox1.selecteditem = =NULL) { return; } intid = Convert.ToInt32 (listBox1.SelectedItem.ToString (). Split ('-')[0]); T_user Model= entity. T_user.where (A = a.ID = =ID). FirstOrDefault (); if(Model! =NULL) {model. Name= DateTime.Now.ToString ("YYYY-MM-DD Hh:mm:ss"); Entity. Entry (model). State=entitystate.modified; Entity. SaveChanges (); Query (); } } /// <summary> ///Enquiry/// </summary> Private voidQuery () {listBox1.Items.Clear (); varExpr = fromPinchEntity. T_userSelectp; foreach(varIteminchexpr) {LISTBOX1.ITEMS.ADD (string. Format ("{0}-{1}", item. Id, item. Name)); } } Private voidToolstripbutton1_click (Objectsender, EventArgs e) {ADD (); } Private voidToolstripbutton4_click (Objectsender, EventArgs e) {Edit (); } Private voidToolstripbutton3_click (Objectsender, EventArgs e) {Delete (); } Private voidToolstripbutton2_click (Objectsender, EventArgs e) {Query (); } }}
The database first of the Entity framework series