实体框架。代码第一。与之间的表的关系
我有一个类似于此 示例。不是学生课程场景,而是两个表,它们有一个与它们相关的共同第三个表。
我的案例是 Items、PerformanceGraphItems 和 PerformanceGraphSeries。
每个项目可以有多个 GraphItem,一个 GraphItem 属于一个 GraphSeries 行。我如何在实体框架中对其进行建模,以便能够访问属于某个项目的 GraphSeries?
像这样的东西。
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<PerformanceGraphItem> PerformanceGraphItems { get; set; }
public virtual ICollection<PerformanceGraphSeries> PerformanceGraphSeries { get; set; }
}
public class PerformanceGraphItem
{
[Key]
public int Id { get; set; }
[ForeignKey("Item")]
public int ItemId { get; set; }
public int SeriesId { get; set; }
public short Year { get; set; }
public double RateOfReturn { get; set; }
public virtual Item Item { get; set; }
[ForeignKey("SeriesId")]
public virtual PerformanceGraphSeries PerformanceGraphSeries { get; set; }
}
public class PerformanceGraphSeries
{
public int id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<PerformanceGraphItem> PerformanceGraphItems { get; set; }
}
I have a Database similar to the one in this example. Not the Student-courses scenario, but 2 tables that have a common third related to both of them.
My case is, Items, PerformanceGraphItems and PerformanceGraphSeries.
Each item can have several GraphItems and a GraphItem belongs to a GraphSeries row. How could I model it in the Entity Framework in order to be able to access the GraphSeries that belong to an Item?
Something like this.
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<PerformanceGraphItem> PerformanceGraphItems { get; set; }
public virtual ICollection<PerformanceGraphSeries> PerformanceGraphSeries { get; set; }
}
public class PerformanceGraphItem
{
[Key]
public int Id { get; set; }
[ForeignKey("Item")]
public int ItemId { get; set; }
public int SeriesId { get; set; }
public short Year { get; set; }
public double RateOfReturn { get; set; }
public virtual Item Item { get; set; }
[ForeignKey("SeriesId")]
public virtual PerformanceGraphSeries PerformanceGraphSeries { get; set; }
}
public class PerformanceGraphSeries
{
public int id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<PerformanceGraphItem> PerformanceGraphItems { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,你所拥有的与你想要的非常接近,尽管我会
将你的线路更改为
or
根据你的情况
。 Thius 是因为您并没有真正对
Item
和PerformanceGraphSeries
之间有直接连接的数据库进行建模 - 因此您不希望以这种方式生成数据库。但这仍然允许您在一个属性中访问它(而不必在任何地方执行该查询)。另外,由于您在大多数地方都使用
虚拟
,我假设您正在尝试使用代理对象 - 确保您将所有属性设置为虚拟的,否则就不会发生。It seems to me that what you have is pretty close to what you want, though I would change your
line to instead be
or
depending on your scenario.
Thius is because you're not really modeling a database that has a direct connect between
Item
andPerformanceGraphSeries
- so you don't want your database to be generated that way. But this still allows you to access it in one property (instead of having to do that query everywhere).Also, since you're using
virtual
in most places, I assume you're trying to go for proxy objects - make sure you're setting all of your properties to be virtual, otherwise it won't happen.