Entity Framework Fluent API

Source: Internet
Author: User

It is very simple to use DataAnnotation. However, for the features in EntityFramework, EntityFramework assembly must be introduced in the Entity class. However, it is best to keep the POCO class unrelated to the architecture to make it more universal. Therefore, it is best to use FluentAPI in the data layer to map object classes to databases. In terms of functions, Data Annotations is a subset of Fluent APIs. All functions that Data Annotations can implement can be implemented by Fluent APIs.

Of course, the DataAnnotation of the System. ComponentModel. DataAnnotations namespace also has the corresponding API in the EntityFramework assembly:

Set the attribute in the database operation Context

        protected override void OnModelCreating(DbModelBuilder modelBuilder)        {            base.OnModelCreating(modelBuilder);        }

1. HasKey-KeyAttribute: configure the primary key attribute of this object type

            modelBuilder.Entity<Order>().HasKey(order => order.OrderID);

2. IsRequired-RequiredAttribute: set this attribute as required. The database column used to store this attribute cannot be null.

modelBuilder.Entity<Order>().HasRequired(order => order.OrderName);

3. HasMaxLength-MaxLengthAttribute: Set the attribute to have the specified maximum length.

modelBuilder.Entity<Order>().Property(order => order.OrderName).HasMaxLength(30);

4. IsConcurrencyToken-concurrencycheckattriken: configure the attribute as an open concurrency tag

modelBuilder.Entity<Order>().Property(order => order.Address).IsConcurrencyToken();

5. IsRowVersion-TimestampAttribute: Set the attribute to the row version in the database. The actual data type varies with the database provider. When you set the attribute to the row version, the attribute is automatically configured as an open concurrency tag.

modelBuilder.Entity<Order>().Property(order => order.TimeStamp).IsRowVersion();

The above APIs do not need to reference EntityFramework. We recommend that you use DataAnnotation to set mappings. The DataAnnotation feature of the following APIs is defined in EntityFramework. If you use the DataAnnotation method to set ing, additional third-party Assembly dependencies will be added to the object class. Therefore, FluentAPI is recommended for the following API mappings.

6. ToTable-TableAttribute: name of the table mapped to this object type

modelBuilder.Entity<Order>().ToTable(, );

7. HasColumnName-ColumnAttribute: Specifies the name of the database column used to store the attribute.

modelBuilder.Entity<Order>().Property(order => order.Note).HasColumnName().HasColumnType();

8. HasForeignKey-ForeignKeyAttribute: configure the link to use the foreign key attribute in the object model. If the foreign key attribute is not public in the object model, the Map method is used.

modelBuilder.Entity<Order>().HasRequired(order => order.customer).WithMany().HasForeignKey(order => order.CustomerNo);

9. Ignore-NotMappedAttribute: queues an attribute from the model so that the attribute is not mapped to the database.

modelBuilder.Entity<Order>().Ignore(order => order.PhotoPath);

10. HasRequired: use this object type to configure the required relationship. Unless this relationship is specified, entity-type instances cannot be saved to the database. The foreign key in the database cannot be null.

modelBuilder.Entity<Order>().HasRequired(order => order.customer);

11. Map: configure the link to use a foreign key attribute not published in the object model. You can customize columns and tables by specifying configurations. If an empty configuration operation is specified, a column name is generated. If the foreign key attribute is disclosed in the object model, the HasForeignKey method is used. Not all links support public foreign key attributes in the object model.

modelBuilder.Entity<Order>().HasRequired(c => c.customer).WithMany().Map(m => m.MapKey("CustomerOrder"));

12. MapKey: name of the column for configuring the foreign key.

Same as above ..

 

 

 

Related Article

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.