如何创建有2个引用到另一个类POCO对象 [英] How do I create a POCO object that has 2 references to another class
问题描述
我有一个表(事件),可以有2个位置(主,备用)。位置可以在其它表中(所以在locationTable无事件ID)使用POCO自跟踪,我怎么创建从事件表到位置表,或什么是处理这种情况的最好方法参考使用(我有一个总的脑冻过这一点)? (.NET 4.0 C#,EF4.1,MVC 3中使用)。
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).
简体类:
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;}
}
我想每个表和指示,如果它是主要的或ALT位置的标志PK的链接表(EventLocations),但我不知道如何做到这一点看在POCO类设置。也许这一点,但它需要额外的业务逻辑(我最终希望能够将这一王解成T4,所以我不必在今后的项目添加业务逻辑):
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!
推荐答案
最简单的方法就是使用:
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
).
这篇关于如何创建有2个引用到另一个类POCO对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!