Click here to Skip to main content
15,069,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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")]
        public string Name { get; set; }

        public bool IsSubscribedToNewsLetter { get; set; }
        public MembershipType MembershipType { get; set; }

        [Display(Name = "Date of Birth")]
        public DateTime? Birthdate { get; set; }
        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; }
        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.
Updated 1-Feb-17 17:14pm

1 solution

codegeekalpha 2-Feb-17 11:07am
I think the question i asked.. you did not understand...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900