Entity Framework (EF – còn được gọi là OR/M Framework) làm việc ở mức trừu tượng cao hơn LINQ to SQL. Khi làm việc với EF một nhà phát triển phải tạo ra một lược đồ khái niệm (conceptual schema), một lược đồ lưu trữ (store schema), và một ánh xạ (mapping) giữa hai lược đồ này. Trong LINQ to SQL mỗi lớp sẽ ánh xạ đến chính xác một bảng, trong EF mỗi lớp có thề ánh xạ đến nhiều bảng.

LINQ to Entities cho phép các nhà phát triển truy vấn đến mô hình dữ liệu khái niệm và các truy vấn là các truy vấn LINQ chuẩn.

Các bước tạo và thực thi một truy vấn LINQ to Entities:

  • Tạo một thể hiện ObjectQuery<T> từ ObjectContext.
  • Tạo một truy vấn LINQ to Entities bằng cách dùng thể hiện ObjectQuery<T>.
  • Chuyển các toán tử truy vấn LINQ chuẩn và các biểu thức sang cây lệnh (command trees).
  • Thực thi truy vấn và khi ngoại lệ xuất hiện sẽ được chuyển trực tiếp đến client.
  • Trả kết quả truy vấn trở lại client.

ObjectContext là lớp chính cho phép tương tác với Entity Data Model hay nói cách khác nó như chiếc cầu nối kết nối LINQ và cơ sở dữ liệu. Cây lệnh (command tree) là một hình thức truy vấn tương thích với Entity Framework.

Thêm mô hình Entity vào dự án (VB hay C#) như ví dụ sau (dùng lại cơ sở dữ liệu ngocminhADO với hai bảng GenreReview):

  • Tạo dự án Console Appliction
  • Kích chuột phải vào dự án và chọn Add > New Item

  • Trong cửa sổ Add New Item chọn ADO.NET Entity Data Model như sau:

  • Nhấn nút Add và chọn Generate from database và nhấn Next:

  • Chọn database và nhấn Next (chú ý ngocminhADOEntities):

  • Chọn tất cả các bảng từ databaseFinish:

Thêm (Add), Xoá (Delete), Sửa (Update) dùng LINQ to Entities:

Hiển thị dữ liệu từ bảng Genre (Mã C#):


using (ngocminhADOEntities context = new ngocminhADOEntities())

{

  //Hiển thị dữ liệu từ Genre

  var GenreList = from d in context.Genres

  select d;

  foreach (var gen in GenreList)

  {

    Console.WriteLine("Department Id = {0} ,

           Department Name = {1}", gen.ID, gen.Name);

  }

}

Thêm dữ liệu vào bảng Genre (102, Country)


using (ngocminhADOEntities context = new ngocminhADOEntities())

{

  //Hiển thị dữ liệu từ Genre

  var GenreList = from d in context.Genres

  select d;

  Console.WriteLine("Danh sach ban dau:");

  foreach (var gen in GenreList)

  {

   Console.WriteLine("Department Id = {0} , Department Name =

         {1}", gen.ID, gen.Name);

  }

  //thêm dữ liệu vào bảng Genre

  Genre newgen = new Genre();

  newgen.ID = 102;

  newgen.Name = "Country";

  context.Genres.Add(newgen);

  context.SaveChanges();

  Console.WriteLine("Danh sach sau khi them:");

  foreach (var gen in GenreList)

  {

   Console.WriteLine("Department Id = {0} , 

             Department Name = {1}", gen.ID, gen.Name);

  }

}

Kết quả:

Chỉnh sửa dữ liệu từ bảng Genre (102, Rock)


using (ngocminhADOEntities context = new ngocminhADOEntities())

{

  //Hiển thị dữ liệu từ Genre

  var GenreList = from d in context.Genres

  select d;

  Console.WriteLine("Danh sach ban dau:");

  foreach (var gen in GenreList)

  {

   Console.WriteLine("Department Id = {0} , 

          Department Name = {1}", gen.ID, gen.Name);

  }

  //chỉnh sửa dữ liệu từ bảng Genre

  Genre updatedgen = context.Genres.FirstOrDefault(d => d.ID == 102);

  updatedgen.Name = "Rock";

  context.SaveChanges();

  Console.WriteLine("Danh sach sau khi them:");

  foreach (var gen in GenreList)

  {

   Console.WriteLine("Department Id = {0} , 

        Department Name = {1}", gen.ID, gen.Name);

  }

}

Kết quả:

Xoá dữ liệu từ bảng Genre (102, Rock)


using (ngocminhADOEntities context = new ngocminhADOEntities())

{

  //Hiển thị dữ liệu từ Genre

  var GenreList = from d in context.Genres

  select d;

  Console.WriteLine("Danh sach ban dau:");

  foreach (var gen in GenreList)

  {

   Console.WriteLine("Department Id = {0} , 

        Department Name = {1}", gen.ID, gen.Name);

  }

  //xoá dữ liệu từ bảng Genre

  Genre deletedgen = context.Genres.FirstOrDefault(d =>

  d.ID == 102);

  context.Genres.Remove(deletedgen);

  context.SaveChanges();

  Console.WriteLine("Danh sach sau khi xoa:");

  foreach (var gen in GenreList)

  {

    Console.WriteLine("Department Id = {0} , 

        Department Name = {1}", gen.ID, gen.Name);

  }

}

Kết quả:

< Học LINQ