Entity Framework Code First convention, entityframework

Source: Internet
Author: User

Entity Framework Code First convention, entityframework

Code First enables you to describe the model through C # Or Visual Basic. NET. The Basic rules of the model are checked by conventions, and conventions are a series of built-in rules.

In Code First, the conceptual model is automatically configured through a series of Rule Conventions based on the class definition. The Conventions are defined in the namespace System. Data. Entity. ModelConfiguration. Conventions.

You can further configure your model through data annotation or fluent API.

The data annotation convention is followed by the fluent API configuration first. For more information, see Data Annotations, Fluent API-Relationships, Fluent API-Types & Properties and Fluent API with VB. NET.

For a series of Code First conventions, refer to API Documentation. The topic of this article mainly describes the Code First conventions.

Type Exploration

When we use Code First for development. NET Framework class to define the concept (domain) model. In addition to defining the class, you also need to let DbContext know which types are the models you want to represent. Therefore, you need to define a context class that inherits from DbContext and use DbSet to modify the type you need to represent as a model. Code First will include these types and obtain the reference type, even if the reference is defined in different assemblies.

If your class is in the inherited structure system and these classes are under the same assembly, it is enough to define the base class as a DbSet attribute, it will automatically infer other associated types.

public class SchoolEntities : DbContext {     public DbSet<Department> Departments { get; set; } }  public class Department {     // Primary key     public int DepartmentID { get; set; }     public string Name { get; set; }      // Navigation property     public virtual ICollection<Course> Courses { get; set; } }  public class Course {     // Primary key     public int CourseID { get; set; }      public string Title { get; set; }     public int Credits { get; set; }      // Foreign key     public int DepartmentID { get; set; }      // Navigation properties     public virtual Department Department { get; set; } }      public partial class OnlineCourse : Course {     public string URL { get; set; } }  public partial class OnsiteCourse : Course {     public string Location { get; set; }     public string Days { get; set; }     public System.DateTime Time { get; set; } }

If you do not want to use a certain type as a model class, you can use the NotMapped attribute or DbModelBuilder. Ignore.

ModelBuilder. Ignore <Department> ();

Primary key conventions

If an attribute named "ID" (case-insensitive) exists in the class or the class name + "ID", Code First will automatically deduce this attribute as the primary key.

public class Department {     // Primary key     public int DepartmentID { get; set; }      . . .   }
Relational conventions

In Entity Framework, the navigation property provides a jump between two object types. The navigation property allows you to navigate and manage relationships in two directions. It returns a referenced object (one or zero objects) or a set (Multiple object lists ). Code First is based on the navigation property to infer the direct relationship of the type.

In addition to the navigation property, we recommend that you add a foreign key property to indicate the direct dependency of the object.

The following format indicates a foreign key relationship: <navigation property name> <primary key name of the navigation Object>, <Class Name of the navigated Object> <primary key name of the navigated Object> or <primary key name of the navigated Object>. If Multiple matching relationships are found, it will be inferred according to the sequence given above. Foreign key detection is case insensitive.

When a foreign key attribute is detected, Code First infers the relationship based on an empty foreign key. If the attribute is left blank, the link is optional. Otherwise, the link must be registered.

If the object on which the foreign key depends cannot be empty, Code First sets the cascading deletion relationship. If the object on which the foreign key depends is empty, Code First does not set cascading deletion relationships. When the referenced object is deleted, the foreign key is set to null. For more information about cascading deletion, You can reset the conventions by using fluent API.

In the following example, the navigation property and foreign key are used to define the relationship between the Department and the Course class.

public class Department {     // Primary key     public int DepartmentID { get; set; }     public string Name { get; set; }      // Navigation property     public virtual ICollection<Course> Courses { get; set; } }  public class Course {     // Primary key     public int CourseID { get; set; }      public string Title { get; set; }     public int Credits { get; set; }      // Foreign key     public int DepartmentID { get; set; }      // Navigation properties     public virtual Department Department { get; set; } }

NOTE: If there are multiple relationships between the same types (for example, if you have defined the Person and Book classes, the Person class contains the ReviewedBooks and AuthoredBooks navigation attributes, at the same time, the Book class also contains the Author and Reviewer navigation attributes. You need to manually configure the relationship through Data Annotations or the fluent API. For more information, see Data Annotations-Relationships and Fluent API-Relationships.

Replication type conventions

When Code First finds that the primary key of a class cannot be inferred and is not identified by data annotations or fluent APIs, this type is automatically treated as a complex type, complex types require that they do not have attributes that reference other object types.

The following class Details will be treated as a complex type because it does not set a primary key

public partial class OnsiteCourse : Course {     public OnsiteCourse()     {         Details = new Details();     }      public Details Details { get; set; } }  public class Details {     public System.DateTime Time { get; set; }     public string Location { get; set; }     public string Days { get; set; } }
Connection string conventions

Learning

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.