1

I have a table (Event) that can have 2 Locations (main, alternate). Locations can be used in other tables (so no EventId in the locationTable) Using POCO self-tracking, how do I create the reference from the Event table to the Locations table, or what is the best way to handle this situation (I'm having a total brain freeze over this)? (.NET 4.0 C#, EF4.1, MVC 3 being used).

Simplified classes:

public class Event
{
   public int EventId {get; set;}
   public string Tile {get; set;}
   public int MainLocationId {get; set;}
   public int AltLocationId {get; set;}

   public virtual ICollection<Location> EventLocations {get; set;}
}

public class Location
{
   public int LocationId {get; set;}
   public string Name {get; set;}
}

I was thinking a linking table (EventLocations) with the PK of each table and a flag indicating if it's the main or alt location, but I'm not sure how this would look in a POCO class setup. Maybe this, but it requires extra business logic (and I ultimately would like to be able to incorporate this king of solution into a T4 so I don't have to add business logic in future projects):

public class Event
{
   public int EventId {get; set;}
   public string Tile {get; set;}

   public virtual ICollection<EventLocation> EventLocations {get; set;}
}

public class Location
{
   public int LocationId {get; set;}
   public string Name {get; set;}

   public virtual ICollection<EventLocation> EventLocations {get; set;}
}

public class EventLocation
{
   public int EventId {get;set;}
   public int LocationId {get;set;}
   public bool IsMain {get; set;}

   public virtual Event Event {get;set;}
   public virtual Location Location {get;set;}
}

Thanks for any advice/ tips/ solutions/ constructive criticism!

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
Wayne W.
  • 117
  • 7

1 Answers1

1

The simplest way is simply using:

public class Event
{
   public int EventId {get; set;}
   public string Tile {get; set;}
   public int MainLocationId {get; set;}
   public int AltLocationId {get; set;}

   public virtual Location MainLocation {get; set;}
   public virtual Location AlternativeLocation {get; set;}

   // You can also add Foreign key properties for locations to simplify some usage scenarios
}

public class Location
{
   public int LocationId {get; set;}
   public string Name {get; set;}
}

You have exact number of locations defined for your event so using many-to-many relation is probably not needed (it can accidentally add more locations to your Event).

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • You know, that's just so obvious. sometimes you think about something just too long and miss the easy obvious solution. but wouldn't I need to make the reference in the Event class an IEnumerable for the 1-to-many? – Wayne W. May 06 '11 at 22:33
  • eh, nevermind that question. I thought about it on the drive home! it makes sense and I see where I was looking at it wrong. thanks! – Wayne W. May 09 '11 at 17:42