Configure One-to-One (Configure a One-to-One relationship) [Code-First series], codefirst One-to-One

Source: Internet
Author: User

Configure One-to-One (Configure a One-to-One relationship) [Code-First series], codefirst One-to-One

Now, I want to learn how to configure a one-to-one relationship. As we all know, the one-to-one relationship is the primary key in one table. In another table, both the primary key and the foreign key are [in fact one-to-zero or one-to-one ].

Note: The one-to-one relationship is technically impossible in ms SQL Server. It is mainly a one-to-zero or one-to-one relationship.

For more information about entity relationships, see MSDN. ----- >>> Entity Relationship.

1. Use the data Annotation Feature to configure a one-to-one (one-to-zero) relationship.

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF5{   public class Student    {       public int StudentID { get; set; }       public string StudentName { get; set; }       public virtual StudentAddress StudentAddress { get; set; }    }}
Using System; using System. collections. generic; using System. componentModel. dataAnnotations; using System. componentModel. dataAnnotations. schema; using System. linq; using System. text; namespace EF5 {public class StudentAddress {// the parameters in the ForeignKey attribute fill in the navigation attribute. [Key, ForeignKey ("Student")] public int StudentID {get; set;} public string Address1 {get; set;} public string Address2 {get; set ;} public string City {get; set;} public int Zipcode {get; set;} public string State {get; set;} public string Country {get; set ;} public virtual Student {get; set ;}}}
using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF5{   public class DbContextClass:DbContext    {       public DbContextClass() : base("ConnectionString")        {           Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DbContextClass>());       }       public DbSet<Student> Students { get; set; }       public DbSet<StudentAddress> StudentAddresses { get; set; }           }}

 

Database:

Note that in the above Code, Student entity does not do anything. Then, the Code-First default convention uses StudentID as the table's primary key, and then in the StudentAddress entity, I have specifically specified the Key and ForeignKey features to mark StudentId as both a primary Key and a foreign Key. In the ForeignKey feature, I specified the Student object parameter, so it forms a one-to-one relationship.

Of course, we can use the Fluent API to configure a one-to-one relationship:

Use the Fluent APi to configure the one-to-one (one-to-zero) relationship

Using System; using System. collections. generic; using System. data. entity; using System. linq; using System. text; using System. threading. tasks; namespace EF5 {public class DbContextClass: DbContext {public DbContextClass (): base ("ConnectionString") {Database. setInitializer (new DropCreateDatabaseIfModelChanges <DbContextClass> ();} public DbSet <Student> Students {get; set;} public DbSet <StudentAddress> StudentAddresses {get; set ;} protected override void OnModelCreating (DbModelBuilder modelBuilder) {// configure the table's primary key modelBuilder. entity <StudentAddress> (). hasKey (s => s. studentID); // configure the table's foreign key modelBuilder. entity <StudentAddress> (). hasRequired (s => s. student ). witexceptional (p => p. studentAddress); base. onModelCreating (modelBuilder );}}}

Please note: the following code is incorrectly written and reversed !!!

Using System; using System. collections. generic; using System. data. entity; using System. linq; using System. text; using System. threading. tasks; namespace EF5 {public class DbContextClass: DbContext {public DbContextClass (): base ("ConnectionString") {Database. setInitializer (new DropCreateDatabaseIfModelChanges <DbContextClass> ();} public DbSet <Student> Students {get; set;} public DbSet <StudentAddress> StudentAddresses {get; set ;} protected override void OnModelCreating (DbModelBuilder modelBuilder) {// configure the table's primary key modelBuilder. entity <StudentAddress> (). hasKey (s => s. studentID); // configure the table's foreign key // modelBuilder. entity <StudentAddress> (). hasRequired (s => s. student ). witexceptional (p => p. studentAddress); modelBuilder. entity <StudentAddress> (). hasOptional (s => s. student ). withRequired (p => p. studentAddress); base. onModelCreating (modelBuilder );}}}

The database is as follows:

 

The following section describes how to configure a one-to-many relationship.

 

 

Attached series directory:

  • What is Code First?
  • Simple Code First example
  • Code-First Convention
  • DB Initialization (Database Initialization)
  • Inheritance Strategy (Inheritance Policy)
  • Configure Domain Classes (configuration Domain class)
  • DataAnnotations (Data annotation)
  • Fluent API
  • Configure One-to-One (Configure a One-to-One relationship)
  • Configure One-to-multiple (Configure One-to-multiple relationships)
  • Configure allow-to-Configure (Configure Many-to-Many relationships)
  • Move deployments (data migration)
  • DB Initialization Strategy (Database Initialization Policy)

 

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.