EntityFramWork (3 code First Convention), entityframworkcode

Source: Internet
Author: User

EntityFramWork (3 code First Convention), entityframworkcode

 

Code First convention

With Code First, you can use the C # Or Visual Basic. NET class to describe the model. The basic shape of the model can be checked by convention. A convention is a rule set that is used to automatically configure the conceptual model based on class definitions when Code First is used. Conventions are defined in the System. Data. Entity. ModelConfiguration. Conventions namespace.

You can use data annotations or Fluent APIs to further configure the model. The priority is configured through the Fluent API, followed by data annotations, and again through conventions. For more information, see Data annotation, Fluent API-relation, Fluent API-type and attribute, and use Fluent API in VB. NET.

The API documentation provides a detailed list of Code First conventions. This topic describes the conventions used by Code First.

Type discovery

When Code First is used for development, it usually starts from the. NET Framework class used to define the concept (domain) model. In addition to defining classes, you also needDbContextKnow the classes to be included in the model. Therefore, you need to define a context class, which is derived fromDbContextMake public the types that need to be part of the modelDbSetAttribute. Code First includes these types and any reference types, even if they are defined in different sets of programs.

If the type exists in the inheritance hierarchy, it is defined as the base class.DbSetThe property is sufficient. If the derived type is in the same assembly as the base class, the derived types are automatically included.

In the following exampleSchoolEntitiesClass DefinitionDbSetProperty (Administrative ments). Code First uses this property to discover and contain any reference types.

Public ClassSchoolEntities: DbContext
{
PublicDbSet <Department> Departments {get; set ;}
}
 
Public ClassDepartment
{
// Primary key
Public IntInclumentid {get; set ;}
Public StringName {get; set ;}
 
// Navigation property
Public VirtualICollection <Course> Courses {get; set ;}
}
 
Public ClassCourse
{
// Primary key
Public IntCourseID {get; set ;}
 
Public StringTitle {get; set ;}
Public IntCredits {get; set ;}
 
// Foreign key
Public IntInclumentid {get; set ;}
 
// Navigation properties
Public VirtualDepartment {get; set ;}
}

PublicPartialClassOnlineCourse: Course
{
Public StringURL {get; set ;}
}
 
PublicPartialClassOnsiteCourse: Course
{
Public StringLocation {get; set ;}
Public StringDays {get; set ;}
PublicSystem. DateTime Time {get; set ;}
}

If you want to exclude the type from the model, useNotMappedFeature orDbModelBuilder. IgnoreFluent API.

ModelBuilder. Ignore <Department> ();

Primary key conventions

If the attribute name of a class is "ID" (case-insensitive) or "ID" is followed by the class name, Code First will infer that this attribute is the primary key. If the type of the primary key attribute is numeric or GUID, configure it as the ID column.

Public ClassDepartment
{
// Primary key
Public IntInclumentid {get; set ;}
 
...
 
}

Relational conventions

The navigation attribute in the object framework provides a way to navigate between two object types. For every relation in which an object participates, each object can have a navigation attribute. Using navigation properties, you can navigate and manage relationships in two directions, and return referenced objects (if multiple objects are one or zero or one) or collections (if multiple objects are multiple ). Code First deduce the relationship based on the navigation attribute defined for the type.

In addition to navigation attributes, we recommend that you include foreign key attributes that indicate the type of the dependent object. Any attribute of the same data type as the primary key property of the subject, which follows the following format represents the foreign key of the link: "<navigation property name> <subject primary key property>", "<Subject Class Name> <primary key property name>" or "<subject primary key property name> ". If multiple matching items are found, the priority is in the order listed above. The foreign key detection is case insensitive. When detecting foreign key attributes, Code First is based on the null property of foreign keys to infer the multiple links. If the attribute can be Null, the link is registered as an optional link; otherwise, the link is registered as a required link.

If the foreign key on the dependent object cannot be Null, Code First sets cascade deletion for the link. If the foreign key on the dependent object can be Null, Code First does not set cascading deletion for the link, and the foreign key is set to Null when the subject is deleted. By using the Fluent API, You can overwrite the multiple and cascading deletion behaviors detected by conventions.

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

Public ClassDepartment
{
// Primary key
Public IntInclumentid {get; set ;}
Public StringName {get; set ;}
 
// Navigation property
Public VirtualICollection <Course> Courses {get; set ;}
}
 
Public ClassCourse
{
// Primary key
Public IntCourseID {get; set ;}
 
Public StringTitle {get; set ;}
Public IntCredits {get; set ;}
 
// Foreign key
Public IntInclumentid {get; set ;}
 
// Navigation properties
Public VirtualDepartment {get; set ;}
}

Note:If there are multiple relationships between the same types (for example, assume that PersonAndBookClass, where,PersonIncludeReviewedBooksAndAuthoredBooksNavigation properties, whileBookClass inclusionAuthorAndReviewerYou need to manually configure the link using data annotations or Fluent APIs. For more information, seeData annotation-relationship and Fluent API-relationship.

Complex Type conventions

When Code First finds that the primary key cannot be inferred and classes that do not pass the data annotation or Fluent API to register the primary key, the types are automatically registered as complex types. Complex type detection also requires that the type does not have an attribute that references the object type and is not referenced by a collection attribute of another type. For the following class definitions, Code First is inferredDetailsIs a complex type because it does not have a primary key.

PublicPartialClassOnsiteCourse: Course
{
PublicOnsiteCourse ()
{
Details =NewDetails ();
}
 
PublicDetails {get; set ;}
}
 
Public ClassDetails
{
PublicSystem. DateTime Time {get; set ;}
Public StringLocation {get; set ;}
Public StringDays {get; set ;}
}

Connection string conventions

To learn about the conventions that DbContext uses to discover the connections to use, see connection and model.

Remove conventions

You can remove any Conventions defined in the System. Data. Entity. ModelConfiguration. Conventions namespace. The following example is removed.PluralizingTableNameConvention.

Public ClassSchoolEntities: DbContext
{
...
 
Protected Override VoidOnModelCreating (DbModelBuilder modelBuilder)
{
// Configure Code First to ignore PluralizingTableName convention
// If you keep this convention, the generated tables
// Will have pluralized names.
ModelBuilder. Conventions. Remove <PluralizingTableNameConvention> ();
}
}

Pluggable conventions

The pluggable (custom) conventions are currently not supported and are being developed for ef6.

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.