如何创建一个有两个引用到另一个类的POCO对象
问题描述:
我有一个可以有两个位置(主,备用)的表(事件)。位置可以在其他表中使用(所以在locationTable中没有EventId)使用POCO自我跟踪,如何创建从事件表到位置表的引用,或者处理这种情况的最佳方式是什么脑部全面冻结)? (使用.NET 4.0 C#,EF4.1,MVC 3)。如何创建一个有两个引用到另一个类的POCO对象
简体类:
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;}
}
我想每个表的PK和指示,如果是主或ALT位置的国旗的链接表(EventLocations),但我不知道该怎么会查看POCO课程设置。也许这一点,但它需要额外的商业逻辑(我最终希望能够将这一国王的解决方案为T4,所以我不必在未来的项目中添加业务逻辑):
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;}
}
谢谢对于任何建议/提示/解决方案/建设性的批评!
答
最简单的方法就是使用:
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;}
}
你有那么使用为您的活动定义的位置确切数量多到很多可能是不需要的关系(它可能会意外地增加更多位置,以您的Event
)。
你知道,这很明显。有时你会考虑太长时间的事情,而错过简单明了的解决方案。但是我不需要在Event类中引用IEnumerable为1对多? –
2011-05-06 22:33:51
呃,没关系那个问题。我在回家的路上考虑过了!这是有道理的,我看到我在哪里看错了。谢谢! – 2011-05-09 17:42:00