日期:2014-05-20  浏览次数:21014 次

Ado.Net Entities Framework实例

新建库MyTestDB,在库下面新建表tb_Student,表字段定义如下图所示。

 

新建控制台应用程序MyTestDB

在项目下新建ADO.NET实体数据模型。

在项目上右击--添加--新建项--选择“数据”--选择“ADO.NET实体数据模型”--输入名称--点击“添加”按钮

 

选择“从数据库生成”模型,点击“下一步”按钮

 

选择数据库连接,点击“下一步”按钮(若没有数据库连接,请新建连接)

 

选择数据库对象,设置模型命名空间,点击“完成”按钮。

 

查看项目文件组织结构,里面多出App.ConfigMyTestDB.edmx两个文件。

 

 

添加测试代码

using System;
using System.Linq;
 
namespace MyTestDB
{
    public class Program
    {
        static void Main(string[] args)
        {
            AddNewStudent();//向数据库增加记录:wsp 20 1 80
            SelectStudent();//查询classid为1的记录,得到:wsp 20 1 80
            UpdateStudent();//修改wsp的年龄为22
            DeleteStudent();//删除wsp的信息
        }
 
        /// <summary>
        /// 新增学生记录
        /// </summary>
        public static void AddNewStudent()
        {
            using(MyTestDBEntities myDbEntity =new MyTestDBEntities())
            {
                tb_Student tbStudent=new tb_Student();
                tbStudent.name = "wsp";
                tbStudent.age = 20;
                tbStudent.classid = 1;
                tbStudent.score = 80;
                myDbEntity.AddTotb_Student(tbStudent);
                int count = myDbEntity.SaveChanges();
            }
        }
 
        /// <summary>
        /// 删除学生记录
        /// </summary>
        public static void DeleteStudent()
        {
            using (MyTestDBEntities myDbEntity = new MyTestDBEntities())
            {
                var query = from student in myDbEntity.tb_Student
                            where student.name == "wsp"
                            select student;
                if (query != null)
                {
                    foreach (var q in query)
                    {
                        myDbEntity.DeleteObject(q);
                    }
                    myDbEntity.SaveChanges();
                }
            }
        }
 
        /// <summary>
        /// 更新学生记录
        /// </summary>
        public static void UpdateStudent()
        {
            using (MyTestDBEntities myDbEntity = new MyTestDBEntities())
            {
                var query = from student in myDbEntity.tb_Student
                            where student.name == "wsp"
                            select student;
                if (query != null)
                {
                    foreach (var q in query)
                    {
                        q.age = 22;
                    }
                    myDbEntity.SaveChanges();
                }
            }
        }
 
        /// <summary>
        /// 查询学生记录
        /// </summary>
        public static void SelectStudent()
        {
            using (MyTestDBEntities myDbEntity = new MyTestDBEntities())
            {
                var query = from student in myDbEntity.tb_Student
                            where student.classid == 1
                            select student;
                if (query != null)
                {
                    foreach (var q in query)
                    {
                        Console.WriteLine(
                            q.name + " " 
                            + q.age + " " 
                            + q.classid + " " 
                            + q.score);
                    }
                }
            }
        }
    }
}

该实例完成了一个简单的增删改查的操作,不涉及 Ado.Net Entities Framework理论知识的介绍。