Configure One-to-One Relationship:
We are going to configure a One-to-One relationship between Student and StudentAddress. As you may know, a one-to-one relationship happens when the primary key of one table becomes PK & FK in another table. Here, StudentId is a Primary key of Student table so StudentId should be PK and FK in StudentAddress table in order to have one-to-one (actually one-to-zero or one) relationship between them.Note that a one-to-one relationship is technically not possible in MS SQL Server. It will always be one-to-zero or one.
Visit Entity Relationship section to understand how EF manages one-to-one, one-to-many, and many-to-many relationships.
Configure one-to-zero or one relationship using DataAnnotations:
The following example shows one-to-one relationship is configured using DataAnnotations attributes.public class Student { public Student() { } public int StudentId { get; set; } public string StudentName { get; set; } public virtual StudentAddress StudentAddress { get; set; } } public class StudentAddress { [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 Student { get; set; } }As you can see in the above Student and StudentAddress class, we haven't done anything special in Student class because it follows the code first conventions, so StudentId will become PK. Now, we have used the Key and ForeignKey attributes for StudentId property in StudentAddress class, in order to mark it as PK as well as FK. Notice that we have specified Student entity in the ForeignKey attribute. Thus, Code-First will create a one-to-one relationship between Student and StudentAddress using DataAnnotations attributes.
Configure One-to-Zero-or-One relationship using Fluent API:
The following example sets one-to-zero or one relationship between Student and StudentAddress using Fluent API.protected override void OnModelCreating(DbModelBuilder modelBuilder) { // Configure StudentId as PK for StudentAddress modelBuilder.Entity<StudentAddress>() .HasKey(e => e.StudentId); // Configure StudentId as FK for StudentAddress modelBuilder.Entity<Student>() .HasOptional(s => s.StudentAddress) // Mark StudentAddress is optional for Student .WithRequired(ad => ad.Student); // Create inverse relationship }As you can see in the above example, we set primary key to StudentId for StudentAddress because it doesn't follow the Code-First conventions for PK. In the second line, HasOptional method makes StudentAddress property nullable and then WithRequired method creates inverse relationship by making StudentId column as FK in StudentAddress table. Thus, StudentId will be PK and FK in StudentAddress which makes one-to-zero or one relationship.
Alternatively, you can also configure StudentAddress entity, as shown below.
protected override void OnModelCreating(DbModelBuilder modelBuilder) { // Configure StudentId as PK for StudentAddress modelBuilder.Entity<StudentAddress>() .HasKey(e => e.StudentId); // Configure StudentId as FK for StudentAddress modelBuilder.Entity<StudentAddress>() .HasRequired(ad => ad.Student) .WithOptional(s => s.StudentAddress); }So, DataAnnotations and Fluent API example for one-to-zero or one relationship will create the following database:
You can check the relationship between Student and StudentAddress in the database, as shown below:
If you create an entity data model of a created database then it will appear like the diagram shown below:
Learn how to create a one-to-many relationship in the next section.
Hiç yorum yok:
Yorum Gönder