第一步 新建数据库和表
USE [TestDB] GO /****** Object: Table [dbo].[T_User] Script Date: 01/14/2015 20:27:52 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[T_User]( [Id] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](50) NULL, [Password] [nchar](10) 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
第二步 ADO.NET实体数据模型
新建ADO.NET实体数据模型
程序自动生成以下文件
TestModel.edmx
TestModel.Context.tt
TestModel.Context.tt是生成数据操作实体类的模板
namespace EFDEMO1 { using System; using System.Data.Entity; using System.Data.Entity.Infrastructure; public partial class TestDBEntities : DbContext { public TestDBEntities() : base("name=TestDBEntities") //TestDBEntities为App.config中的数据库连接字符串 { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public DbSet<T_User> T_User { get; set; } } }
TestModel.Designer.cs
TestModel.edmx.diagram
TestModel.tt
TestModel.tt模板是生成T_User实体类的模板
namespace EFDEMO1 { using System; using System.Collections.Generic; public partial class T_User { public int Id { get; set; } public string Name { get; set; } public string Password { get; set; } } }
修改数据库
当修改数据库对象(如表),只需要在TestModel.edmx界面上右键点击从数据库更新模型,更新完成后保存即可自动生成相关代码(如实体类)。
第三步 增删改查
using System; using System.Data; using System.Linq; using System.Windows.Forms; namespace EFDEMO1 { public partial class Form1 : Form { TestDBEntities entity = new TestDBEntities(); public Form1() { InitializeComponent(); } /// <summary> /// 新增 /// </summary> private void Add() { T_User model = new T_User() { Name = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") }; entity.T_User.Add(model); entity.SaveChanges(); Query(); } /// <summary> /// 删除 /// </summary> private void Delete() { if (listBox1.SelectedItem == null) { return; } int id = 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> /// 修改 /// </summary> private void Edit() { if (listBox1.SelectedItem == null) { return; } int id = 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> /// 查询 /// </summary> private void Query() { listBox1.Items.Clear(); var expr = from p in entity.T_User select p; foreach (var item in expr) { listBox1.Items.Add(string.Format("{0}-{1}", item.Id, item.Name)); } } private void toolStripButton1_Click(object sender, EventArgs e) { Add(); } private void toolStripButton4_Click(object sender, EventArgs e) { Edit(); } private void toolStripButton3_Click(object sender, EventArgs e) { Delete(); } private void toolStripButton2_Click(object sender, EventArgs e) { Query(); } } }