{"id":86,"date":"2026-06-10T03:10:24","date_gmt":"2026-06-10T03:10:24","guid":{"rendered":"https:\/\/csharp.ihavenomoney.co.kr\/netcore\/?p=86"},"modified":"2026-06-10T04:32:34","modified_gmt":"2026-06-10T04:32:34","slug":"system-componentmodel-dataannotations","status":"publish","type":"post","link":"https:\/\/csharp.ihavenomoney.co.kr\/netcore\/?p=86","title":{"rendered":"System.ComponentModel.DataAnnotations"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>\ub370\uc774\ud130 \uc720\ud6a8\uc131 \uac80\uc0ac(Validation)<\/strong> \uc640 <strong>\ubaa8\ub378 \uba54\ud0c0\ub370\uc774\ud130(Metadata)<\/strong> \ub97c \uc81c\uacf5\ud558\ub294 .NET \ub77c\uc774\ube0c\ub7ec\ub9ac\uc785\ub2c8\ub2e4.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uc8fc\ub85c \ub2e4\uc74c\uacfc \uac19\uc740 \uc6a9\ub3c4\ub85c \uc0ac\uc6a9\ud569\ub2c8\ub2e4.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\ub124\uc784\uc2a4\ud398\uc774\uc2a4<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\uc785\ub825\uac12 \uac80\uc99d<\/li>\n\n\n\n<li>API Request \uac80\uc99d<\/li>\n\n\n\n<li>ASP.NET MVC \/ Web API \ubaa8\ub378 \uac80\uc99d<\/li>\n\n\n\n<li>Entity Framework \ubaa8\ub378 \uc81c\uc57d\uc870\uac74 \uc815\uc758<\/li>\n\n\n\n<li>\ud654\uba74 \ud45c\uc2dc(Display) \uc815\ubcf4 \uc815\uc758<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \">using System.ComponentModel.DataAnnotations;<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">1. Required<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\ud544\uc218 \uc785\ub825\uac12<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \">public class Passenger\n{\n    [Required]\n    public string Name { get; set; }\n}<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\uac80\uc99d:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \">var p = new Passenger();\n\nvar context = new ValidationContext(p);\nvar results = new List&lt;ValidationResult&gt;();\n\nbool isValid = Validator.TryValidateObject(\n    p,\n    context,\n    results,\n    true);<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\uacb0\uacfc<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Name \ud544\ub4dc\ub294 \ud544\uc218\uc785\ub2c8\ub2e4.<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. StringLength<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\ubb38\uc790 \uae38\uc774 \uc81c\ud55c<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \">public class Passenger\n{\n    [StringLength(20)]\n    public string Name { get; set; }\n}<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\ucd5c\uc18c\/\ucd5c\ub300 \uae38\uc774<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \">[StringLength(20, MinimumLength = 2)]\npublic string Name { get; set; }<\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. MaxLength<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\ucd5c\ub300 \uae38\uc774<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \">[MaxLength(50)]\npublic string Email { get; set; }<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. MinLength<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\ucd5c\uc18c \uae38\uc774<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \">[MinLength(2)]\npublic string Name { get; set; }<\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. Range<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\uc22b\uc790 \ubc94\uc704<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \" >[Range(1, 999)]\npublic int Quantity { get; set; }<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\uc608:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \" >[Range(0, 100)]\npublic int Score { get; set; }<\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. EmailAddress<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\uc774\uba54\uc77c \ud615\uc2dd \uac80\uc0ac<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \" >[EmailAddress]\npublic string Email { get; set; }<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\uc608:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \" >abc@test.com  \u2192 OK\nabc           \u2192 \uc624\ub958<\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7. Phone<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\uc804\ud654\ubc88\ud638 \ud615\uc2dd<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \" >[Phone]\npublic string Mobile { get; set; }<\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">8. Url<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">URL \ud615\uc2dd<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \" >[Url]\npublic string Website { get; set; }<\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">9. RegularExpression<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\uc815\uaddc\uc2dd \uac80\uc0ac<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uc608: PNR 6\uc790\ub9ac<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \" >[RegularExpression(@\"^[A-Z0-9]{6}$\")]\npublic string PNR { get; set; }<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\ud5c8\uc6a9:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \" >ABC123\nGY459S<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\ubd88\ud5c8:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \" >ABC-123\nABCDEFG<\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">10. Display<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\ud654\uba74 \ud45c\uc2dc\uba85<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \" >[Display(Name = \"\uc608\uc57d\ubc88\ud638\")]\npublic string PNR { get; set; }<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">11. DisplayFormat<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\ucd9c\ub825 \ud615\uc2dd<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \" >[DisplayFormat(DataFormatString = \"{0:yyyy-MM-dd}\")]\npublic DateTime IssueDate { get; set; }<\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">12. Key<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Entity Framework \uae30\ubcf8\ud0a4<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \" >public class Ticket\n{\n    [Key]\n    public int Id { get; set; }\n}<\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">13. Custom Validation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\uc9c1\uc811 \uac80\uc99d \ub85c\uc9c1 \uc791\uc131<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \" >public class Ticket\n{\n    [CustomValidation(typeof(TicketValidator),\n                      nameof(TicketValidator.ValidateTicket))]\n    public string TicketNo { get; set; }\n}<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\ub370\uc774\ud130 \uc720\ud6a8\uc131 \uac80\uc0ac(Validation) \uc640 \ubaa8\ub378 \uba54\ud0c0\ub370\uc774\ud130(Metadata) \ub97c \uc81c\uacf5\ud558\ub294 .NET \ub77c\uc774\ube0c\ub7ec\ub9ac\uc785\ub2c8\ub2e4. \uc8fc\ub85c \ub2e4\uc74c\uacfc \uac19\uc740 \uc6a9\ub3c4\ub85c \uc0ac\uc6a9\ud569\ub2c8\ub2e4. \ub124\uc784\uc2a4\ud398\uc774\uc2a4 1. Required \ud544\uc218 \uc785\ub825\uac12 \uac80\uc99d: \uacb0\uacfc 2. StringLength \ubb38\uc790 \uae38\uc774 \uc81c\ud55c \ucd5c\uc18c\/\ucd5c\ub300 \uae38\uc774 3. MaxLength \ucd5c\ub300 \uae38\uc774 4. MinLength \ucd5c\uc18c \uae38\uc774 5. Range \uc22b\uc790 \ubc94\uc704 \uc608: 6. EmailAddress \uc774\uba54\uc77c \ud615\uc2dd \uac80\uc0ac \uc608: 7. Phone \uc804\ud654\ubc88\ud638 \ud615\uc2dd 8. Url URL\u2026 <span class=\"read-more\"><a href=\"https:\/\/csharp.ihavenomoney.co.kr\/netcore\/?p=86\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-86","post","type-post","status-publish","format-standard","hentry","category-1"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/netcore\/index.php?rest_route=\/wp\/v2\/posts\/86","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/netcore\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/netcore\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/netcore\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/netcore\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=86"}],"version-history":[{"count":4,"href":"https:\/\/csharp.ihavenomoney.co.kr\/netcore\/index.php?rest_route=\/wp\/v2\/posts\/86\/revisions"}],"predecessor-version":[{"id":93,"href":"https:\/\/csharp.ihavenomoney.co.kr\/netcore\/index.php?rest_route=\/wp\/v2\/posts\/86\/revisions\/93"}],"wp:attachment":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/netcore\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/netcore\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/netcore\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}