Entity Framework DataAnnotations

Source: Internet
Author: User

The DataAnnotation feature is introduced in. NET 3.5, providing a method for adding verification to classes in. NET. However, in EF, ing relationships can be controlled, which is easier to use than Fluent APIs.

DataAnnotation is provided by the namespace System. ComponentModel. DataAnnotations. The following lists common DataAnnotation features in object models:

1. KeyAttribute: Set the primary key of the table in the database

        [Key]        public int OrderID { get; set; }

2. RequiredAttribute: Can the data of fields in the database be null?

        [Required]        public string OrderName { get; set; }

3. MaxLengthAttribute: specifies the maximum length of string fields in the database.

        [MaxLength(60)]        public string Employee{get;set;}

4. MinLengthAttribute: the minimum length of the string in the Code is not matched in the database.

        [MaxLength(60),MinLength(10)]        public string Employee{get;set;}

5. concurrencycheckattri: Specifies the Data Type of the columns used for the open concurrency check.

        [ConcurrencyCheck]        public string Address { get; set; }

6. timestampattri: Specify the column data type as the row version.

        1384619419        public byte[] TimeStamp { get; set; }

The System. ComponentModel. DataAnnotations namespace only defines the features of some entity verification. More Data ing features are defined in the EntityFramework program.

7. databaseGeneratedAttribute: indicates that the specified object attribute is generated by the database, and specifies the generation policy (None database does not generate a value. When Identity is inserted, the database generates a value. When Computed is inserted or updated, database generated value)

        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]        public DateTime CreateDate { get; set; }

8. ColumnAttribute: Specifies the column name and Data Type of the object attribute in the database.

        [Column("Notes", TypeName = "ntext")]        public string Note { get; set; }

9. TableAttribute: name of the data table corresponding to the object class

    [Table("Order",Schema="Order")]    public class Order

10. ForeignKeyAttribute: Specifies the foreign key field of the navigation attribute.

    public class Customer    {        public int ID { get; set; }        public string Name { get; set; }    }        [ForeignKey("ID")]        public Customer customer { get; set; }

11. NotMappedAttribute: indicates that the specified object property is not created in the database creation.

        [NotMapped]        public string PhotoPath { get; set; }

12. ComplexTypeAttribute: marking a specified object attribute is the attribute of an object as another object. When mapped to a database, the sub-object is represented as multiple attribute fields.



public class Name { public string FirstName { get; set; } public string LastName { get; set; } } public Name Name { get; set; }

The data table relationships corresponding to object relationships are nothing more than ", 0: N, 1: N, N: N", which can be expressed by the Data Type in the navigation attribute, 0... One end uses a single object type table, and N end represents the ICollection <T> set type. For a single object, the default value can be null, that is, the relationship is 0. If you want to set it to 1, use the [Required] label to mark it. However, it is impossible to control the subject and dependent object of a one-to-one relationship in more detail.

Note: DataAnnotations can use multiple tag attributes on the latter attributes of the same class at the same time, in the above example, only one unique tag attribute is used for each class or attribute to make it easier to describe; in addition, in the declared example, "ConcurrencyCheck" and "TimeStamp" are used to specify different columns for demonstration. Generally, we can use one of these methods.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.