Quan hệ một – một hoặc zero trong Entity Framework

Cấu hình quan hệ 1 – 0..1 trong Entity Framework code first không cần phải sử dụng API mapping

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; }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s