0

I am completely new to ASP.NET and was trying to cross-reference two different models with each other. I have 'Customer':

public class Customer
    {
        public int CustomerID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
        public string Zipcode { get; set; }
        public string City { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
    }

and 'Reservation':

public class Reservation
    {
        public int ReservationID { get; set; }
        public string PetName { get; set; }
        public DateTime Birthdate { get; set; }
        public string Specie { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public Customer Customer { get; set; }
    }

Now, each Reservation should belong to a Customer (as you can see in the bottom line of the Reservation model) and should therefore contain the CustomerID. On the other hand should each Customer contain references to each Reservation that was made by him.

How can I setup this relation?

jordanhill123
  • 4,142
  • 2
  • 31
  • 40
Severin
  • 8,508
  • 14
  • 68
  • 117
  • 1
    Is this for Entity Framework? Are you asking how to do the C# code, or how to do the database? – mason Sep 25 '14 at 18:35
  • I currently dont have a DB connected yet, so I am mainly interested in the C#. – Severin Sep 25 '14 at 18:36
  • 1
    You've already done it for the Reservation class, now just add a Reservation property to the Customer class. Of course, this is assuming a 1 to 1 relationship. – mason Sep 25 '14 at 18:41
  • Each customer can make multiple reservations, so its a one to many relationship. – Severin Sep 25 '14 at 18:42
  • 1
    Add something like `ICollection Reservations {get; set;}` to the `Customer` – Mike B Sep 25 '14 at 18:44

1 Answers1

1

Use an ICollection for a one to many relationship.

public class Customer
    {
    public int CustomerID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public string Zipcode { get; set; }
    public string City { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public ICollection<Reservation> Reservations { get; set; }
    }

Note I originally suggested a generic list, but there's a few subtle differences.

Community
  • 1
  • 1
mason
  • 31,774
  • 10
  • 77
  • 121