如何创建一个具有 2 个对另一个类的引用的 POCO 对象

发布于 2024-11-05 05:52:18 字数 1323 浏览 0 评论 0原文

我有一个表(事件),可以有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔嚣张 2024-11-12 05:52:18

最简单的方法就是简单地使用:

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;}
}

您已经为您的活动定义了确切数量的位置,因此可能不需要使用多对多关系(它可能会意外地向您的活动添加更多位置)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文