[EntityFramework CodeFirst easy to get started] (5) Example: Use CodeFirst data session and entity class[EntityFramework CodeFirst easy to get started] (2) Example: Install CodeFirst development environment

table of Contents

related articles

[EntityFramework CodeFirst easy to get started] (1) Make the data persistence of the class easier

[EntityFramework CodeFirst easy to get started] (2) Example: Install CodeFirst development environment

[EntityFramework CodeFirst easy to get started] (3) Example: Create a new CodeFirst entity class

[Easily get started with EntityFramework CodeFirst] (4) Example: Create a new CodeFirst database session [DbContext, DbSet]

[EntityFramework CodeFirst easy to get started] (5) Example: Use CodeFirst data session and entity class

Contents of this chapter

1. Example: Initialize the CodeFirst data session instance

2. Example: Create a new CodeFirst entity class instance

3. Example: Query CodeFirst entity class instance

4. Example: Modify the CodeFirst entity class instance

5. Example: Delete CodeFirst entity class instance



1. Example: Initialize the CodeFirst data session instance

When the company first runs, it will automatically create a data structure in the back-end database. By default, the full name of the context class is used as the database name.

code show as below:

    class Program    {        static void Main(string[] args)        {            using (Company company = new Company())            {                 var q = from m in company.Department                         where m.Code == "001"                        select m;                 if (q.Count() > 0)                {                    foreach (var m in q)                    {                        Console.WriteLine(m.Id + "," + m.Code + "," + m.Name);                    }                  }                         Console.ReadLine();        }    }

Execution result: The generated data structure is shown in the figure. The database name CompanyDb is customized through the data connection string.

2. Example: Create a new CodeFirst entity class instance

code show as below:

using (Company company = new Company()){         // 新增实体实例         var department = new Department();        department.Code = "001";        department.Name = "财务部";         company.Department.Add(department);         var department2 = new Department();        department2.Code = "002";        department2.Name = "采购部";         company.Department.Add(department2);          var staff = new Staff();        staff.Code = "111";        staff.Name = "张麻子";        staff.Department = department;         company.Staff.Add(staff);         staff = new Staff();        staff.Code = "222";        staff.Name = "汤师爷";        staff.Department = department;         company.Staff.Add(staff);         staff = new Staff();        staff.Code = "333";        staff.Name = "黄四郎";        staff.Department = department2;         company.Staff.Add(staff);         staff = new Staff();        staff.Code = "444";        staff.Name = "老六";        staff.Department = department2;         company.Staff.Add(staff);         company.SaveChanges(); }

Execution result: The data line is as shown in the figure:

3. Example: Query CodeFirst entity class instance

code show as below:

using (Company company = new Company()){    //查询满足条件的部门    var q = from m in company.Department             where m.Code == "001" || m.Name == "采购部"             select m;    var departments = q.ToList();     if (q.Count() > 0)    {        foreach (var department in departments) //遍历部门查询结果        {            Console.WriteLine("部门:" + department.Id + "," + department.Code + "," + department.Name);            foreach (var m in department.Staffs) //遍历部门的员工            {                Console.WriteLine(" - 员工:" + m.Id + "," + m.Code + "," + m.Name);            }         }     }  }

Results of the:

部门:1,001,财务部 - 员工:1,111,张麻子 - 员工:2,222,汤师爷部门:2,002,采购部 - 员工:3,333,黄四郎 - 员工:4,444,老六

4. Example: Modify the CodeFirst entity class instance

code show as below:

using (Company company = new Company()){    //查询并修改一个员工的信息,包括所在部门                       var q = from m in company.Staff            where m.Name == "老六"             select m;     if (q.Count() > 0)    {        var m = q.First();        m.Name = "张默";        m.Department = company.Department.Where(n => n.Code == "001").First();    }     //查询并修改一个员工的信息,包括所在部门    q = from m in company.Staff            where m.Code == "333"            select m;     if (q.Count() > 0)    {        var m = q.First();        m.Name = "周润发";        m.Department = company.Department.Where(n => n.Code == "001").First();    }     company.SaveChanges(); //批量保存修改  }

Execution result: (re-execute the sample 3 code and perform traversal query)

部门:1,001,财务部 - 员工:1,111,张麻子 - 员工:2,222,汤师爷 - 员工:3,333,周润发 - 员工:4,444,张默部门:2,002,采购部

5. Example: Delete CodeFirst entity class instance

code show as below:

using (Company company = new Company()){    //查询并删除一个员工    var q = from m in company.Staff            where m.Code == "333"            select m;     if (q.Count() > 0)    {        company.Staff.Remove(q.First());    }     //根据Id直接删除一个部门及其员工    var q2 = from m in company.Department                where m.Code == "001"                select m;     company.Department.Remove(q2.First());     company.SaveChanges(); //批量保存修改  }

Execution result: (After executing example 4 and example 5, re-execute the code of example 3 and perform traversal query)

部门:2,002,采购部

Note: By default, when deleting a department, the necessary Department.Staffs members will also be deleted in cascade.

the above