Everyone knows that in a relational database, each field of each table has its own attributes, such as data type, length, whether or not it is empty, primary foreign key, index, and the relationship between tables. But for c # class, it is only a data attribute types and classes and the relationships between classes, but in My Blog and not to any configuration between them, can use the existing database to run My Blog, in the subsequent for Post type to add new attributes, a new attribute also correctly added to the database table, MySQL > select * from ‘MySQL’;
IsPublish and ClickCount were added later via EF Mirgations. SQL > alter table Post (); SQL > alter table Post (); SQL > alter table Post ();
This article will show you how to map relationships between classes to a database from the following two aspects:
● About EF Code first conventions ● Modify properties and relationships through the Fluent API ● Modify properties and relationships using data attribute tags
About EF Code First conventions
Conventions are a set of rules that automatically configure a “conceptual model” based on entity types when using EF Code First. In this case, the “conceptual model” can be interpreted as a database table model. They are in the System. Data. The Entity. ModelConfiguration. Conventions namespace. Here are some common conventions: ● Conventions on IDS (primary key conventions) : Names of types that are named after or end in ID, such as ID or PostID. If the type is numeric or GUID, it will be considered an Identity column. ● Conventions on class relationships (foreign key conventions between tables) : Use the navigation property (even if that property is another entity type) to determine one-to-one, one-to-many, and many-to-many relationships between classes.
Here is an example of Microsoft documentation:
1 public class Department 2 {3 public int DepartmentID {get; set; } 5 public string Name { get; set; } 6 6 public virtual ICollection<Course> Courses {get; set; } 9} 10 11 public class CourseID {13 public int CourseID {get; set; } 15 16 public string Title { get; set; } 17 public int Credits { get; set; } 17 public int DepartmentID {get; set; } 21 22 public virtual Department Department {get; set; 24}}Copy the code
View Code
You are advised to add a foreign key attribute to the dependency type. The following methods are considered to be case insensitive: ○ Navigation attribute name + primary key name of the primary table ○ Primary class name + Primary key name ○ Primary key name of the primary table
Note: If the foreign key attribute is of non-null type, THEN EF establishes an association delete, and when the primary key record is deleted, the associated foreign key record is also deleted. A navigation property is considered a complex type if there is no primary key in the type. More references: msdn.microsoft.com/en-us/libra… Msdn.microsoft.com/library/sys…
Modify properties and relationships through the Fluent API
In addition to the default conventions, you can also use features to mark the corresponding entity attributes and configure the relationship and attribute modification through EF’s Fluent API. This series of articles will focus on Code First, so the mapping of the relationship between entities and databases will also be introduced through the Fluent API using Code. Code First Fluent API is generally inherit DbContext and rewrite OnModelCreating method, in OnModelCreating method through modelBuilder to configure the corresponding entity information:
● Add primary key:
● Set column properties:
● Index creation (EF6.1 version support)
For more information: msdn.microsoft.com/en-us/libra… Use Fluent API to handle relationship reference: msdn.microsoft.com/en-us/libra…
Modify properties and relationships using data attribute tags
In addition to using the Fluent API to modify the mapping between entities and database tables, you can also use data attribute tags to implement relationships between types and database tables and field attributes, but doing so would be invasive to entity code, which should be clean and ef-independent.
You can refer to the documentation on how to use features to modify properties and relationships: msdn.microsoft.com/en-us/libra…
summary
This chapter introduces how to deal with the relationship between entities and database structure, and introduces the main API, but because the database structure in My Blog is relatively simple and cannot be fully demonstrated, so more use methods can refer to the Microsoft documentation.
Reference:
Msdn.microsoft.com/en-us/libra… Msdn.microsoft.com/library/sys… Msdn.microsoft.com/en-us/libra… Msdn.microsoft.com/en-us/libra…
This paper addresses: www.cnblogs.com/selimsong/p…
ASP.NET has no magic — directories