{"id":193,"date":"2026-01-14T21:03:39","date_gmt":"2026-01-14T21:03:39","guid":{"rendered":"https:\/\/blogs.giamkichsan.com\/?p=193"},"modified":"2026-01-14T21:03:40","modified_gmt":"2026-01-14T21:03:40","slug":"the-hien-rang-buoc-khoa-ngoai-foreign-key-trong-entity-framework-ef-ef-core","status":"publish","type":"post","link":"https:\/\/blogs.giamkichsan.com\/index.php\/2026\/01\/14\/the-hien-rang-buoc-khoa-ngoai-foreign-key-trong-entity-framework-ef-ef-core\/","title":{"rendered":"Th\u1ec3 hi\u1ec7n r\u00e0ng bu\u1ed9c kh\u00f3a ngo\u1ea1i (Foreign Key) trong Entity Framework (EF \/ EF Core)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u2705 C\u00e1ch 1 (Khuy\u1ebfn ngh\u1ecb): Convention (Theo quy \u01b0\u1edbc EF)<\/h2>\n\n\n\n<p>EF t\u1ef1 nh\u1eadn di\u1ec7n Foreign Key n\u1ebfu b\u1ea1n \u0111\u1eb7t t\u00ean \u0111\u00fang quy \u01b0\u1edbc.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">V\u00ed d\u1ee5: Quan h\u1ec7 1\u2013n (User \u2013 Orders)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class User\n{\n    public int Id { get; set; }\n    public string Name { get; set; }\n\n    public ICollection&lt;Order&gt; Orders { get; set; }\n}\n\npublic class Order\n{\n    public int Id { get; set; }\n\n    \/\/ Foreign Key\n    public int UserId { get; set; }\n\n    \/\/ Navigation property\n    public User User { get; set; }\n}\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49 EF s\u1ebd t\u1ef1 hi\u1ec3u:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>UserId<\/code> l\u00e0 kh\u00f3a ngo\u1ea1i<\/li>\n\n\n\n<li><code>Order.User<\/code> li\u00ean k\u1ebft t\u1edbi <code>User.Id<\/code><\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udccc <strong>Kh\u00f4ng c\u1ea7n c\u1ea5u h\u00ecnh th\u00eam<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 C\u00e1ch 2: Data Annotation <code>[ForeignKey]<\/code><\/h2>\n\n\n\n<p>D\u00f9ng khi <strong>t\u00ean FK kh\u00f4ng theo quy \u01b0\u1edbc<\/strong> ho\u1eb7c mu\u1ed1n r\u00f5 r\u00e0ng h\u01a1n.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.ComponentModel.DataAnnotations.Schema;\n\npublic class Order\n{\n    public int Id { get; set; }\n\n    public int CreatedBy { get; set; }\n\n    &#91;ForeignKey(\"CreatedBy\")]\n    public User User { get; set; }\n}\n<\/code><\/pre>\n\n\n\n<p>Ho\u1eb7c ng\u01b0\u1ee3c l\u1ea1i:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Order\n{\n    public int Id { get; set; }\n\n    &#91;ForeignKey(nameof(User))]\n    public int UserId { get; set; }\n\n    public User User { get; set; }\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 C\u00e1ch 3: Fluent API (Chu\u1ea9n &amp; m\u1ea1nh nh\u1ea5t)<\/h2>\n\n\n\n<p>D\u00f9ng khi:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Quan h\u1ec7 ph\u1ee9c t\u1ea1p<\/li>\n\n\n\n<li>Composite Key<\/li>\n\n\n\n<li>C\u1ea5u h\u00ecnh cascade delete, required, optional\u2026<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">V\u00ed d\u1ee5 1\u2013n:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>protected override void OnModelCreating(ModelBuilder modelBuilder)\n{\n    modelBuilder.Entity&lt;Order&gt;()\n        .HasOne(o =&gt; o.User)\n        .WithMany(u =&gt; u.Orders)\n        .HasForeignKey(o =&gt; o.UserId)\n        .OnDelete(DeleteBehavior.Cascade);\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Quan h\u1ec7 1\u20131<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>modelBuilder.Entity&lt;User&gt;()\n    .HasOne(u =&gt; u.Profile)\n    .WithOne(p =&gt; p.User)\n    .HasForeignKey&lt;UserProfile&gt;(p =&gt; p.UserId);\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Quan h\u1ec7 n\u2013n (EF Core 5+)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>modelBuilder.Entity&lt;Student&gt;()\n    .HasMany(s =&gt; s.Courses)\n    .WithMany(c =&gt; c.Students);\n<\/code><\/pre>\n\n\n\n<p>Ho\u1eb7c c\u00f3 b\u1ea3ng trung gian:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>modelBuilder.Entity&lt;StudentCourse&gt;()\n    .HasKey(sc =&gt; new { sc.StudentId, sc.CourseId });<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>\u2705 C\u00e1ch 1 (Khuy\u1ebfn ngh\u1ecb): Convention (Theo quy \u01b0\u1edbc EF) EF t\u1ef1 nh\u1eadn di\u1ec7n Foreign Key n\u1ebfu b\u1ea1n \u0111\u1eb7t t\u00ean \u0111\u00fang quy \u01b0\u1edbc. V\u00ed d\u1ee5: <a class=\"mh-excerpt-more\" href=\"https:\/\/blogs.giamkichsan.com\/index.php\/2026\/01\/14\/the-hien-rang-buoc-khoa-ngoai-foreign-key-trong-entity-framework-ef-ef-core\/\" title=\"Th\u1ec3 hi\u1ec7n r\u00e0ng bu\u1ed9c kh\u00f3a ngo\u1ea1i (Foreign Key) trong Entity Framework (EF \/ EF Core)\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-193","post","type-post","status-publish","format-standard","hentry","category-netcore-lap-trinh-netframework"],"_links":{"self":[{"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/posts\/193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/comments?post=193"}],"version-history":[{"count":1,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/posts\/193\/revisions"}],"predecessor-version":[{"id":194,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/posts\/193\/revisions\/194"}],"wp:attachment":[{"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/media?parent=193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/categories?post=193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/tags?post=193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}