|
¸ñ·ÏÀ¸·Î °¡±â.. |
|
Entity Framework : Data Annotations |
±èÁ¾È£
|
»èÁ¦Çϱâ
|
|
Data AnnotationÀº .NET Framework¿¡¼ Data ValidationÀ» µ½±â À§ÇØ Á¦°øµÇ´Â ±â´ÉÀ¸·Î Ŭ·¡½ºÀÇ ¼Ó¼º¿¡ ¿©·¯ Data Annotation Ư¼º(attribute)¸¦ Ãß°¡ÇÔÀ¸·Î º°µµÀÇ ÄÚµå¾øÀÌ Validation ±ÔÄ¢À» Àû¿ëÇÒ ¼ö ÀÖ´Ù. Entity FrameworkÀÇ DbSet Ŭ·¡½º¿¡¼ ÀÌ·¯ÇÑ Data AnnotationÀ» »ç¿ëÇÏ¿© Å×À̺í Ä÷³¿¡ Ưº°ÇÑ ¼Ó¼ºÀ» ÁöÁ¤ÇÒ ¼ö ÀÖÀ¸¸ç, ¶ÇÇÑ °¢ ÇʵåÀÇ Ãâ·ÂÆ÷¸Ë°ú Ư¼ºµéÀ» ÁöÁ¤ÇÏ¿© ASP.NET MVC¿Í °°Àº ¿ÜºÎ ÇÁ·¹ÀÓ¿öÅ©¿¡¼ ÀÌ·¯ÇÑ Validation Ư¼ºµéÀ» È°¿ëÇÒ ¼ö ÀÖµµ·Ï ÇÏ°í ÀÖ´Ù.
Entity Framework¿¡¼ Å×ÀÌºí ½ºÅ°¸¶¸¦ Á¤ÀÇÇϴ Ŭ·¡½º¸¦ ÀÛ¼ºÇÒ ¶§, Ä÷³Å¸ÀÔ, ÃÖ´ë Å©±â, Identity ¼Ó¼º µî Å×À̺í Á¤ÀÇ¿¡ ÇÊ¿äÇÑ ¿©·¯ ¼Ó¼ºµéÀ» ÇÔ²² ÁöÁ¤ÇÒ ¼ö ÀÖ´Ù. ¶ÇÇÑ, Ŭ·¡½ºÀÇ Çʵå(Ä÷³)°¡ ¿ÜºÎ¿¡ Ç¥½ÃµÉ ¶§, DisplayName À̳ª DisplayFormat °°Àº Ư¼ºµéÀ» »ç¿ëÇÒ ¼ö Àִµ¥, ¿¹¸¦ µé¾î MVC À¥ ÇÁ·¹ÀÓ¿öÅ©¿¡¼ ÀÌ·¯ÇÑ Display Ư¼ºµéÀ» »ç¿ëÇÏ¿© ÇʵåÀÇ Çì´õ³ª Æ÷¸ËÀ» ÀÚµ¿À¸·Î Àû¿ëÇÑ´Ù.
¾Æ·¡ ¿¹Á¦´Â Customer Ŭ·¡½º¿¡ ¿©·¯ Data AnnotationÀ» ºÙ¿©º» »ùÇÃÀÌ´Ù. Entity Framework¿¡¼ Primary Key (PK)¸¦ ÁöÁ¤Çϱâ À§ÇØ, [Key] ¶ó´Â Ư¼ºÀ» ÁöÁ¤ÇÑ´Ù. ¸¸¾à Ŭ·¡½º¿¡ Key Ư¼ºÀÌ ¾øÀ¸¸é Id ȤÀº *Id¿Í °°ÀÌ ³¡ÀÌ Id·Î ³¡³ª´Â Ä÷³À» Primary Key·Î »ç¿ëÇÑ´Ù. Id Ä÷³Àº Identity ·Î ¸¸µé±â À§Çؼ´Â DatabaseGenerated(DatabaseGeneratedOption.Identity) Ư¼ºÀ» ÁöÁ¤ÇÏ¸é µÇ´Âµ¥, À̸¦ »ç¿ëÇϸé Id Ä÷³ÀÌ DB¿¡¼ ÀÚµ¿À¸·Î ä¿öÁø´Ù. PK ÀÌ¿Ü¿¡ Foreign Key¸¦ ÁöÁ¤Çϱâ À§Çؼ´Â [ForeignKey()] ¸¦ »ç¿ëÇϸç, ƯÁ¤ Ä÷³¿¡ À妽º¸¦ »ý¼ºÇϱâ À§Çؼ´Â [Index] Ư¼ºÀ» ÁöÁ¤ÇÑ´Ù.
Ä÷³ÀÌ ¹Ýµå½Ã µ¥ÀÌŸ°¡ ÀԷµǾî¾ß ÇÏ´Â °æ¿ì Áï NULLÀÌ Çã¿ëµÇÁö ¾Ê´Â °æ¿ì [Required] Ư¼ºÀ» ÁöÁ¤ÇÑ´Ù. ¶ÇÇÑ, ¹®ÀÚ¿ÀÇ ÃÖ´ë, ÃÖ¼Ò±æÀ̸¦ Ç¥ÇöÇϱâ À§Çؼ´Â MaxLength(n), MinLength(m) Ư¼ºÀ» ÁöÁ¤ÇÑ´Ù.
¿¹Á¦
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
public class Customer
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
[StringLength(100)]
[Index(IsUnique = true)]
public string LoginId { get; set; }
[Display(Name = "Create Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTimeOffset CreateDate { get; set; }
[Required]
[MaxLength(100)]
public string Name { get; set; }
[EmailAddress]
public string Email { get; set; }
public int Age { get; set; }
}
ÀÚÁÖ »ç¿ëµÇ´Â Data Annotation
Data Annotation | ¼³¸í | ¿¹Á¦ |
Table |
Table ¼Ó¼º ÁöÁ¤. Table¸í ÁöÁ¤ |
[Table("CustTab")] public class Customer |
Column |
Column ¼Ó¼º ÁöÁ¤ |
[Column("Misc", typename="ntext")] public string Data {get; set; }
|
Key |
Primary Key ÁöÁ¤ |
[Key] public int Id { get; set; }
PK°¡ º¹¼ö Ä÷³ÀÎ °æ¿ì:
[Key] [Column(Order=1)] public string FirstName { get; set; }
[Key] [Column(Order=2)] public string Last { get; set; }
|
ForeignKey |
Foreign Key |
[ForeignKey("Id")] public int EmpId { get; set; } |
Index |
Index Ä÷³ ÁöÁ¤ (EF 6.1+) |
[Index] public int RefId { get; set; } |
DatabaseGenerated |
DB »ý¼º ¿É¼Ç ÁöÁ¤ |
[DatabaseGenerated(DatabaseGenerationOption.Identity] public int Id { get; set; } |
Required |
Çʼö Ç׸ñ. NOT NULL Çʵå |
[Required] public string Name { get; set; } |
MaxLength |
ÃÖ´ë ±æÀÌ |
[MaxLength(100)] public string Name { get; set; } |
MinLength |
ÃÖ¼Ò ±æÀÌ |
[MinLength(5)] public string Name { get; set; } |
Range |
ÃÖ¼Ò, ÃÖ´ë ¹üÀ§°ªÀ» ÁöÁ¤ |
[Range(0, 100)] public int Score { get; set; } |
NotMapped |
Å×ÀÌºí¿¡ ÀúÀåµÇÁö ¾Ê´Â Çʵå |
[NotMapped] public int Extra { get; set; } |
Timestamp |
Timestamp Ä÷³ |
[Timestamp] public byte[] TimeStamp { get; set; } |
Email |
Email ÁÖ¼Ò Ã¼Å© |
[EmailAddress] public string Email { get; set; } |
|
|
|