Entity Framework link constraint Configuration

Source: Internet
Author: User

Simply put, everyone should understand ADO. NET, that is, the original database operation, directly concatenates SQL statements, and links between tables (inner join left join or subquery ), that is, the table is pre-designed and associated with the table through the primary and Foreign keys. So how do I configure and process it in Entity Framework?

Data Table relationships corresponding to object relationships are nothing more than ", 1: N, N: N", which can be expressed by the Data Type in the navigation attribute, 0... One side is represented by a single object type, and N side is represented by an ICollection collection type. For a single object, the default value can be null, that is, the relationship is 0. If you want to set it to 1, use the [Required] label to mark it. However, it is impossible to control the subject and dependent object of a one-to-one relationship in more detail.

Let's take a look at the relationships between these data tables.

First: 0: 1. For example, there is a data table Emplyee representing the employee table of the company, and the employee of the company has a database table MessagingAccount of the communication account, and some employees do not use it, that is, there is no communication account. This indicates that the relationship between Employee and MessagingAccount is that an Employee corresponds to a communication account, or an Employee does not have a communication account.


modelBuilder.Entity<MessagingAccount>().HasRequired(m => m.Employee).WithOptional(n=>n.Acount);

Configure a 〇 relation to allow the existence of an Employee without MessagingAcount (Note: add the Accout attribute to the Employee)

Example 2: 1: 1. In the first example, each employee has a communication account.

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

Type 3: 1: N. Example: The order and who placed the order. Generally, an order is placed by only one person. In other words, a person may have multiple orders. That is, a Customer can have multiple orders. It is a one-to-many relationship.

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

Type 4: N. Example: Relationship between orders and products. An Order may have multiple Product options. Similarly, a Product may also 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 the many-to-many relationship and specify the table name and corresponding foreign keys. Note that if FluentAPI is not used for configuration, the corresponding navigation attributes are configured for Product and Order, EF also generates a table by default (the table name is "<data Class 1> + <data Class 2> ")

Map: configure the link to use a foreign key attribute that is not made public in the object model. You can customize columns and tables by specifying configurations. If an empty configuration operation is specified, a column name is generated. If the foreign key attribute is disclosed in the object model, the HasForeignKey method is used. Not all links support public foreign key attributes in the object model.

Special: Use of the combination of primary keys.

// Combine primary keys
ModelBuilder. Entity <Order> (). HasRequired (o => o. Customer). WithMany (). HasForeignKey (o => new {o. CustomerNo, o. CustomerName });

 

modelBuilder.ComplexType<Name>().Property(m => m.FirstName).HasMaxLength(40);

 

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.