Asp. NET no magic--asp.net MVC and database of EF entity class and database structure

Source: Internet
Author: User
Tags documentation

Everyone knows that in a relational database, each field in a table has its own properties, such as data type, length, whether it is empty, the primary foreign key, the index, and the relationship between the table and tables. However, for a class written in C #, its properties have only one data type and the relationship between classes and classes, but in my blog there is no configuration between them, my blog can use the existing database to run, in the subsequent post type to add new properties, The new properties are also correctly added to the database table, and the following is the MySQL database table structure:

  

Where Ispublish and Clickcount are subsequently added through EF mirgations. Table is created directly from EF based on the Post class, it can be seen that some data types are not the same, the database you create the title and other fields with varchar and EF created is longtext:

  

about EF Code First Conventions

Contract (conventions) is a collection of rules that automatically configures a "conceptual model" based on the entity type when using EF Code First, and the "conceptual model" here can be understood as a database table model. They are located in the System.Data.Entity.ModelConfiguration.Conventions namespace. Here are some common conventions:
Convention on ID ( primary key Convention ): the type is named with an ID or named with an ID, such as ID or PostID. If the type is a number or GUID then it will be considered an identity column.
Conventions on class relationships ( foreign key conventions between tables ): Use navigation properties, which are another entity type, to determine a pair of one or one-to-many and many-to-many relationships between classes and classes.

The following are examples of Microsoft documentation:

1  Public classDepartment2 { 3     //PRIMARY Key4      Public intDepartmentID {Get;Set; } 5      Public stringName {Get;Set; } 6  7     //Navigation Properties8      Public VirtualIcollection<course> Courses {Get;Set; } 9 } Ten   One  Public classCourse A {  -     //PRIMARY Key -      Public intCourseID {Get;Set; }  the   -      Public stringTitle {Get;Set; }  -      Public intCredits {Get;Set; }  -   +     //foreign key (primary table primary key name) -      Public intDepartmentID {Get;Set; }  +   A     //Navigation Properties at      Public VirtualDepartment Department {Get;Set; }  -}
View Code   

It is recommended to add a foreign key attribute in the dependent type, which is considered to be a foreign key attribute (case insensitive):
0 Navigation property name + Primary table primary Key name
0 Main class name + primary Key Name
0 primary Table primary Key name

Note: If the foreign key attribute is non-nullable, EF creates an associated delete, and when the primary key record is deleted, the associated foreign key record is also deleted.
If there is no primary key in the type of the navigation property, it is considered to be a complex type.
More Reference documents:
https://msdn.microsoft.com/en-us/library/jj679962 (v=vs.113). aspx
Https://msdn.microsoft.com/library/system.data.entity.modelconfiguration.conventions.aspx

modifying attributes and relationships through the fluent API

In addition to the default conventions, you can use attributes to mark the corresponding entity attributes and to configure implementation relationships, property modifications through EF's fluent API.
This series of articles is dominated by code first, so the mapping of relationships between entities and databases will also be described using code in the Fluent API.
The Code first Fluent API typically inherits DbContext and overrides the Onmodelcreating method by configuring the corresponding entity information in the Onmodelcreating method through ModelBuilder:

  

To add a primary key:

  

Set Column Properties:

  

Create an index (EF6.1 version support)

    

More Settings Reference: https://msdn.microsoft.com/en-us/library/jj591617 (v=vs.113). aspx
Use the fluent API to process the relationship reference: https://msdn.microsoft.com/en-us/library/jj591620 (v=vs.113). aspx

modifying attributes and relationships using data attribute tags

In addition to using the fluent API to modify the mapping of entities to database tables, you can also use data attribute tagging to implement relationship and field properties between types and database tables, but doing so will invade the entity code, and the entity code should be clean and independent of EF and Database properties.

Refer to Document: https://msdn.microsoft.com/en-us/library/jj591583 (v=vs.113). aspx for information on how to use attributes to modify properties and relationships

Summary:
This chapter describes how the relationship between the entity and the database structure is handled, and introduces the main APIs, but because the database structure in my blog is relatively simple and cannot be fully demonstrated, you can refer to the Microsoft documentation for more usage methods.

Reference:

https://msdn.microsoft.com/en-us/library/jj679962 (v=vs.113). aspx
Https://msdn.microsoft.com/library/system.data.entity.modelconfiguration.conventions.aspx
https://msdn.microsoft.com/en-us/library/jj591617 (v=vs.113). aspx
https://msdn.microsoft.com/en-us/library/jj591620 (v=vs.113). aspx

This address: http://www.cnblogs.com/selimsong/p/7656674.html

Asp. NET no magic--Directory

Asp. NET no magic--asp.net MVC and database of EF entity class and database structure

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.