{"id":2133,"date":"2025-01-31T15:45:08","date_gmt":"2025-01-31T04:45:08","guid":{"rendered":"https:\/\/ntsblog.homedev.com.au\/?p=2133"},"modified":"2025-01-31T15:45:14","modified_gmt":"2025-01-31T04:45:14","slug":"entityframework-class-with-a-composite-key","status":"publish","type":"post","link":"https:\/\/ntsblog.homedev.com.au\/index.php\/2025\/01\/31\/entityframework-class-with-a-composite-key\/","title":{"rendered":"EntityFramework Class with a composite key"},"content":{"rendered":"<div id=\"ntsbl-3259522626\" class=\"ntsbl-before-content ntsbl-entity-placement\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6288941070289539\" crossorigin=\"anonymous\"><\/script><ins class=\"adsbygoogle\" style=\"display:inline-block;width:728px;height:90px;\" \ndata-ad-client=\"ca-pub-6288941070289539\" \ndata-ad-slot=\"9356781486\"><\/ins> \n<script> \n(adsbygoogle = window.adsbygoogle || []).push({}); \n<\/script>\n<\/div>\n<h2 class=\"wp-block-heading\">Issue<\/h2>\n\n\n\n<p>You have a database table with a primary key made up of two columns. This is also called a composite key.<\/p>\n\n\n\n<p>You want to model this in your class using the DataAnnotations Attributes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The solution<\/h2>\n\n\n\n<p>As of the later .Net Core, (I think 6 or above) you need to:<\/p>\n\n\n\n<p>Reference the NuGet Package <\/p>\n\n\n\n<ol class=\"wp-block-list\"><\/ol>\n\n\n\n<p>Looks something like this in your solution file<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">&lt;ItemGroup>\n  &lt;PackageReference Include=\"Microsoft.EntityFrameworkCore\" Version=\"8.0.12\" \/>\n&lt;\/ItemGroup><\/code><\/pre>\n\n\n\n<p>Then create you class as follows<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"csharp\" class=\"language-csharp\">[Table(\"MyTable\")]\n[PrimaryKey(nameof(PKOneId), nameof(PKTwoId))]\npublic class MyTableDto\n{\n    public required int PKOneId { get; set; }\n    public required int PKTwoId{ get; set; }\n    public bool IsActive { get; set; }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to model a c# EntityFramework class with a Primary Key made up of two columns.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[37,5,54],"tags":[],"class_list":["post-2133","post","type-post","status-publish","format-standard","hentry","category-net-core","category-c","category-entityframework"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/2133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/comments?post=2133"}],"version-history":[{"count":0,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/2133\/revisions"}],"wp:attachment":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/media?parent=2133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/categories?post=2133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/tags?post=2133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}