Mapping relationships for Data Objects

Source: Internet
Author: User

is a user information-centric entity Relationship diagram, with the following relationship description:

    1. One user can have an optional user extension information (1-0)
    2. A user extension information has a required user information (0-1)
    3. One user extension information has one user address information (composite type)
    4. One user can correspond to multiple logon log messages (1-n)
    5. A log-in log has a required user information (N-1)
    6. One user can have multiple roles (n-n)
    7. A role can be assigned to multiple users (N-N)

Let's look at the relationship of these data tables

The first type: 0:1. Example: There is a data sheet emplyee on behalf of the Company's staff table, and the company's employees have a Communications Account database table Messagingaccount, and some employees do not use, that is, no communication account. This means that the relationship between employee employees and Messagingaccount is either an employee's correspondence account, or an employee does not have a communication account.

Withoptional: Configures the relationship as Required:optional. (required:0 ... 1 End of 1, indicates required, not for null;optional:0 ... 1 end of 0, which is optional and nullable. Same below

The second kind: 1:1. Example: Or the first example of each employee has a communication account.

Withrequireddependent: Configures the relationship as required:required. The entity type to be configured becomes a dependent object and contains a foreign key for the principal. The entity type that is the target of the relationship becomes the principal in the relationship.

modelBuilder.Entity<Employee>().HasRequired(emp => emp.Acount).WithRequiredPrincipal(a => a.Employee);

The Third Kind: 1:n. Examples: Orders and who orders. The usual relationship is that an order is only made by a single person, which in turn means that a person may have multiple orders. That is, a customer can have multiple order. is a one-to-many relationship.

modelBuilder.Entity<Order>().HasRequired(o=>o.Customer).WithMany();

The fourth kind: N. Example: the relationship between the order and the product. An order may have multiple product products. In the same vein, a product can exist with multiple order orders.

modelBuilder.Entity<Order>().HasMany(o => o.Products).WithMany(p => p.Orders).Map(m =>

{

m.ToTable("OrderDetails");

m.MapLeftKey("OrderID");

m.MapRightKey("ProductID");

});

Configure many-to-many relationships and specify table names, corresponding foreign keys; Note If you do not use the FLUENTAPI configuration, product and order are configured with the corresponding navigation properties, EF will also generate a table by default (table named "< data class 1>+< data Class 2>")

Dataannotation

    1. Keyattribute: The primary key in the corresponding database
    2. RequiredAttribute: Whether the data for a field in the corresponding database can be null
    3. Maxlengthattribute: Maximum length of the String type field in the corresponding database
    4. Minlengthattribute: There is no correspondence in the database, but the minimum length of the string in the code
    5. Concurrencycheckattribute: Specifies the data type of the column used for optimistic concurrency checking
    6. Timestampattribute: Specifying the data type of a column as a row version

The System.ComponentModel.DataAnnotations namespace defines only the attributes of partial entity validation, and more data mapping features are defined in the EntityFramework assembly:

    1. Databasegeneratedattribute: tag specifies that the entity attribute is generated by the database and specifies the build policy (none database does not generate a value, identity when inserting rows, database generates values, computed database generates values when inserting or updating rows)
    2. Columnattribute: Specifies the column name and data type of the entity attribute in the database
    3. Tableattribute: Specifies the data table name for the entity class
    4. Foreignkeyattribute: Specifies the foreign key field of the navigation property
    5. Notmappedattribute: tag specifies that the entity attribute does not create a corresponding field in the CREATE database
    6. Complextypeattribute: tag specifies that an entity attribute is a property of an object as another object, and that the child object is represented as more than one property field when mapped to a database
Fluent API

The dataannotation of the System.ComponentModel.DataAnnotations namespace also has APIs in the EntityFramework assembly:

    1. Haskey-keyattribute: Configure primary key properties for this entity type
    2. Isrequired-requiredattribute: Configures this property as a required property. The database column used to store this property will not be null
    3. Hasmaxlength-maxlengthattribute: Configures the property to have the specified maximum length
    4. Isconcurrencytoken-concurrencycheckattribute: Configuring a property to be used as an optimistic concurrency token
    5. Isrowversion-timestampattribute: Configures the property as a row version in the database. The actual data type will vary depending on the database provider being used. Setting the property to the row version automatically configures the property as an optimistic concurrency token.

  1. Totable-tableattribute: Configure the table name to which this entity type is mapped
  2. Hascolumnname-columnattribute: Configure the name of the database column used to store the property
  3. Hasforeignkey-foreignkeyattribute: Configures the relationship to use foreign key properties in the object model. If the foreign key attribute is not exposed in the object model, use the map method
  4. Ignore-notmappedattribute: A property is queued from the model so that the property is not mapped to the database
  5. Hasrequired: Configures the required relationships through this entity type. An instance of an entity type cannot be saved to the database unless this relationship is specified. Foreign keys in the database cannot be null.
  6. Hasoptional: Configures an optional relationship from this entity type. An instance of an entity type can be saved to the database without specifying this relationship. The foreign key in the database can be null.
  7. Hasmany: Configures a one-to-many relationship from this entity type.
  8. Withoptional: Configures the relationship as Required:optional. (required:0 ... 1 End of 1, indicates required, not for null;optional:0 ... 1 end of 0, which is optional and nullable. Hereinafter )
  9. Withoptionaldependent: Configures the relationship as Optional:optional. The entity type to be configured becomes a dependent object and contains a foreign key for the principal. The entity type that is the target of the relationship becomes the principal in the relationship.
  10. Withoptionalprincipal: Configures the relationship as Optional:optional. The entity type to be configured becomes the principal in the relationship. The entity type that is the target of the relationship becomes the dependent object and contains the foreign key for the principal.
  11. Withrequired: Configures the specified end of the relationship as required and has navigation properties at the other end of the relationship.
  12. Withrequireddependent: Configures the relationship as required:required. The entity type to be configured becomes a dependent object and contains a foreign key for the principal. The entity type that is the target of the relationship becomes the principal in the relationship.
  13. Withrequiredprincipal: Configures the relationship as required:required. The entity type to be configured becomes the entity in the relationship. The entity type that is the target of the relationship becomes the dependent object and contains the foreign key for the principal.
  14. Willcascadeondelete: Configures whether cascading deletes are enabled for the relationship.
  15. Map: Configures the relationship to use foreign key properties that are not exposed in the object model. You can customize columns and tables by specifying configuration actions. If an empty configuration operation is specified, the contract generates a column name. If the foreign key attribute is exposed in the object model, the Hasforeignkey method is used. Not all relationships support exposing foreign key properties in the object model.
  16. Mapkey: Configures the column name of the foreign key.
  17. ToTable: Configures the name and schema of the table where the foreign key column is located.

Frequently used dataannotation and FLUENTAPI listed, use or adhere to this principle:

If there is a corresponding label in the System.ComponentModel.DataAnnotations namespace, use dataannotation and if it does not exist, use the Fluentapi method.

Mapping relationships for Data Objects

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.