实体框架。代码第一。与之间的表的关系

发布于 2024-11-08 03:46:11 字数 1466 浏览 0 评论 0原文

我有一个类似于此 示例。不是学生课程场景,而是两个表,它们有一个与它们相关的共同第三个表。

我的案例是 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 技术交流群。

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

发布评论

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

评论(1

一梦等七年七年为一梦 2024-11-15 03:46:11

在我看来,你所拥有的与你想要的非常接近,尽管我会

public virtual ICollection<PerformanceGraphSeries> PerformanceGraphSeries { get; set; }

将你的线路更改为

public virtual IEnumerable<PerformanceGraphSeries> PerformanceGraphSeries
{
    get
    {
        return PerformanceGraphItems.Select(pgi => pgi.PerformanceGraphSeries);
    }
}

or

public virtual IEnumerable<PerformanceGraphSeries> PerformanceGraphSeries
{
    get
    {
        return PerformanceGraphItems.Select(pgi => pgi.PerformanceGraphSeries)
                                    .Distinct();
    }
}

根据你的情况

。 Thius 是因为您并没有真正对 ItemPerformanceGraphSeries 之间有直接连接的数据库进行建模 - 因此您不希望以这种方式生成数据库。但这仍然允许您在一个属性中访问它(而不必在任何地方执行该查询)。

另外,由于您在大多数地方都使用虚拟,我假设您正在尝试使用代理对象 - 确保您将所有属性设置为虚拟的,否则就不会发生。

It seems to me that what you have is pretty close to what you want, though I would change your

public virtual ICollection<PerformanceGraphSeries> PerformanceGraphSeries { get; set; }

line to instead be

public virtual IEnumerable<PerformanceGraphSeries> PerformanceGraphSeries
{
    get
    {
        return PerformanceGraphItems.Select(pgi => pgi.PerformanceGraphSeries);
    }
}

or

public virtual IEnumerable<PerformanceGraphSeries> PerformanceGraphSeries
{
    get
    {
        return PerformanceGraphItems.Select(pgi => pgi.PerformanceGraphSeries)
                                    .Distinct();
    }
}

depending on your scenario.

Thius is because you're not really modeling a database that has a direct connect between Item and PerformanceGraphSeries - 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.

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