{"id":142,"date":"2017-09-27T16:24:01","date_gmt":"2017-09-27T16:24:01","guid":{"rendered":"http:\/\/td_uid_62_59cbd0a1087a2"},"modified":"2023-03-18T22:50:27","modified_gmt":"2023-03-18T15:50:27","slug":"entity-framework-la-gi","status":"publish","type":"post","link":"https:\/\/thietkewebdalat.net\/blog\/entity-framework-la-gi\/","title":{"rendered":"Entity Framework l\u00e0 g\u00ec"},"content":{"rendered":"<div class=\"td-paragraph-padding-1\">\n<p>Entity Framework l\u00e0 m\u1ed9t framework c\u1ee7a Microsoft \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 x\u00e2y d\u1ef1ng c\u00e1c \u1ee9ng d\u1ee5ng truy c\u1eadp c\u01a1 s\u1edf d\u1eef li\u1ec7u d\u1ef1a tr\u00ean c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng. N\u00f3 cho ph\u00e9p l\u1eadp tr\u00ecnh vi\u00ean truy c\u1eadp d\u1eef li\u1ec7u t\u1eeb c\u01a1 s\u1edf d\u1eef li\u1ec7u b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng v\u00e0 th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c tr\u00ean ch\u00fang, thay v\u00ec s\u1eed d\u1ee5ng c\u00e1c c\u00e2u l\u1ec7nh SQL truy\u1ec1n th\u1ed1ng.<\/p>\n<p>Entity Framework h\u1ed7 tr\u1ee3 nhi\u1ec1u c\u01a1 s\u1edf d\u1eef li\u1ec7u kh\u00e1c nhau, bao g\u1ed3m Microsoft SQL Server, MySQL, Oracle, SQLite, PostgreSQL v\u00e0 nhi\u1ec1u h\u01a1n n\u1eefa. N\u00f3 cung c\u1ea5p cho l\u1eadp tr\u00ecnh vi\u00ean c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng (object) \u0111\u01b0\u1ee3c \u00e1nh x\u1ea1 t\u1eeb c\u01a1 s\u1edf d\u1eef li\u1ec7u (database), gi\u00fap cho vi\u1ec7c thao t\u00e1c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u tr\u1edf n\u00ean d\u1ec5 d\u00e0ng h\u01a1n v\u00e0 ti\u1ebft ki\u1ec7m th\u1eddi gian.<\/p>\n<p>\u0110\u1ec3 minh h\u1ecda, d\u01b0\u1edbi \u0111\u00e2y l\u00e0 m\u1ed9t v\u00ed d\u1ee5 \u0111\u01a1n gi\u1ea3n v\u1ec1 vi\u1ec7c s\u1eed d\u1ee5ng Entity Framework \u0111\u1ec3 thao t\u00e1c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u trong ASP.NET MVC:<\/p>\n<ul>\n<li>T\u1ea1o database:<\/li>\n<\/ul>\n<pre>CREATE DATABASE ExampleDB;\r\nGO\r\nUSE ExampleDB;\r\nGO\r\nCREATE TABLE Customers (\r\n  CustomerID int PRIMARY KEY,\r\n  Name nvarchar(50),\r\n  Email nvarchar(50),\r\n  PhoneNumber nvarchar(50)\r\n);\r\n<\/pre>\n<ul>\n<li>T\u1ea1o project ASP.NET MVC<\/li>\n<li>C\u00e0i \u0111\u1eb7t Entity Framework th\u00f4ng qua NuGet<\/li>\n<\/ul>\n<pre>Install-Package EntityFramework\r\n<\/pre>\n<ul>\n<li>T\u1ea1o l\u1edbp \u0111\u1ed1i t\u01b0\u1ee3ng Customer t\u01b0\u01a1ng \u1ee9ng v\u1edbi b\u1ea3ng Customers trong c\u01a1 s\u1edf d\u1eef li\u1ec7u:<\/li>\n<\/ul>\n<pre>public class Customer\r\n{\r\n    public int CustomerID { get; set; }\r\n    public string Name { get; set; }\r\n    public string Email { get; set; }\r\n    public string PhoneNumber { get; set; }\r\n}\r\n<\/pre>\n<ul>\n<li>T\u1ea1o DbContext \u0111\u1ec3 k\u1ebft n\u1ed1i v\u00e0 qu\u1ea3n l\u00fd c\u01a1 s\u1edf d\u1eef li\u1ec7u:<\/li>\n<\/ul>\n<pre>public class ExampleDBContext : DbContext\r\n{\r\n    public ExampleDBContext() : base(\"ExampleDBConnectionString\")\r\n    {\r\n    }\r\n\r\n    public DbSet Customers { get; set; }\r\n}\r\n<\/pre>\n<ul>\n<li>Th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng Entity Framework:<\/li>\n<\/ul>\n<pre>public class HomeController : Controller\r\n{\r\n    private ExampleDBContext db = new ExampleDBContext();\r\n\r\n    public ActionResult Index()\r\n    {\r\n        \/\/ L\u1ea5y danh s\u00e1ch kh\u00e1ch h\u00e0ng\r\n        var customers = db.Customers.ToList();\r\n        return View(customers);\r\n    }\r\n\r\n    public ActionResult Create()\r\n    {\r\n        return View();\r\n    }\r\n\r\n    [HttpPost]\r\n    public ActionResult Create(Customer customer)\r\n    {\r\n        if (ModelState.IsValid)\r\n        {\r\n            \/\/ Th\u00eam m\u1edbi kh\u00e1ch h\u00e0ng\r\n            db.Customers.Add(customer);\r\n            db.SaveChanges();\r\n            return RedirectToAction(\"Index\");\r\n        }\r\n\r\n        return View(customer);\r\n    }\r\n}\r\n<\/pre>\n<p>\u1ede v\u00ed d\u1ee5 tr\u00ean, ch\u00fang ta \u0111\u00e3 t\u1ea1o m\u1ed9t c\u01a1 s\u1edf d\u1eef li\u1ec7u Customers, t\u1ea1o l\u1edbp \u0111\u1ed1i t\u01b0\u1ee3ng Customer t\u01b0\u01a1ng \u1ee9ng v\u1edbi b\u1ea3ng Customers.<\/p>\n<p>C\u1ea3m \u01a1n c\u00e1c b\u1ea1n \u0111\u00e3 \u0111\u1ecdc b\u00e0i, n\u1ebfu c\u00f3 th\u1eafc m\u1eafc v\u1ea5n \u0111\u1ec1 g\u00ec xin vui l\u00f2ng b\u00ecnh lu\u1eadn ph\u00eda d\u01b0\u1edbi.M\u00ecnh c\u1ea3m \u01a1n \u1ea1.<\/p>\n<div class=\"bg-black mb-4 rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans\"><\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Entity Framework l\u00e0 m\u1ed9t framework c\u1ee7a Microsoft \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 x\u00e2y d\u1ef1ng c\u00e1c \u1ee9ng d\u1ee5ng truy c\u1eadp c\u01a1 s\u1edf d\u1eef li\u1ec7u d\u1ef1a tr\u00ean c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng. N\u00f3 cho ph\u00e9p l\u1eadp tr\u00ecnh vi\u00ean truy c\u1eadp d\u1eef li\u1ec7u t\u1eeb c\u01a1 s\u1edf d\u1eef li\u1ec7u b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng v\u00e0 th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":380,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"amp_status":"","footnotes":""},"categories":[8,11],"tags":[],"class_list":{"0":"post-142","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-net","8":"category-entity-framework"},"_links":{"self":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts\/142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/comments?post=142"}],"version-history":[{"count":3,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":382,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts\/142\/revisions\/382"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/media\/380"}],"wp:attachment":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/media?parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/categories?post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/tags?post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}