List of objects as foreign key
If I have two classes:
public class Event
{
public int EventId { get; set; }
public string EventName { get; set; }
}
and
public class Dog
{
public int DogId { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
I would like my class to event
be able to have a list attached to it dog
. How can I make my event class understandable that I want it to be able to contain a list of dogs? I am using entity framework. Thank!
+3
source to share
1 answer
Assuming you are using Code-First:
public class Event
{
public int EventId { get; set; }
public string EventName { get; set; }
public virtual ICollection<Dog> Dogs { get; set; }
}
public class Dog
{
public int DogId { get; set; }
[ForeignKey("Event")]
public int EventID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public virtual Event Event { get; set; }
}
+2
source to share