I have a table of customers and Membershiptype
There is one to one relationship between customers and membershiptype table
customer table has foreign key of membershiptype table.
I have a code which is quite confusing but works fine to create a foreign key in customers table.
This is class for customer table
public class Customer
{
public int Id { get; set; }
[Required(ErrorMessage="Please Enter Customer's name")]
[StringLength(255)]
public string Name { get; set; }
public bool IsSubscribedToNewsLetter { get; set; }
public MembershipType MembershipType { get; set; }
[Display(Name = "Date of Birth")]
[Min18YearsIfAMember]
public DateTime? Birthdate { get; set; }
[Display(Name="MembershipType")]
public byte MembershipTypeID { get; set; }
}
}
and the class for membershiptype table is
public class MembershipType
{
public byte Id { get; set; }
public string Name { get; set; }
public short SignUpFee { get; set; }
public byte DurationInMonths { get; set; }
public byte DiscountRate { get; set; }
public static readonly byte unknown = 0;
public static readonly byte PayAsYouGo = 1;
}
}
What I have tried:
This most of the tutoirals in internet for one to one relationship use virtual and do not use the above stretegy for creating the forign key
the students table is holding the key for studentaccount table. and is using virtual.
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public virtual StudentAccount StudentAccount { get; set; }
}
The student accounts table
public class StudentAccount
{
public int Id { get; set; }
public string Name { get; set; }
public int Amount { get; set; }
[Required]
public virtual Student Student { get; set; }
}
What is the difference between two methods for these one to one relationships. and how these works in mapping the foreign key.