如何创建一个具有 2 个对另一个类的引用的 POCO 对象
我有一个表(事件),可以有 2 个位置(主位置、备用位置)。位置可以在其他表中使用(因此位置表中没有 EventId)使用 POCO 自跟踪,如何创建从事件表到位置表的引用,或者处理这种情况的最佳方法是什么(我是脑子完全冻结了)? (使用.NET 4.0 C#、EF4.1、MVC 3)。
简化的类:
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;}
}
我正在考虑一个链接表(EventLocations),其中包含每个表的 PK 和一个指示它是主要位置还是备用位置的标志,但我不确定这在 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;}
}
感谢您的任何建议/提示/解决方案/ 建设性的批评!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法就是简单地使用:
您已经为您的活动定义了确切数量的位置,因此可能不需要使用多对多关系(它可能会意外地向您的
活动
添加更多位置)。The simplest way is simply using:
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
).