如何使用实体框架查询外键对象? [英] How can I query the foreign key objects using Entity Framework?

查看:16
本文介绍了如何使用实体框架查询外键对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解 Entity Framework 6,但遇到了一个问题,我已经能够在测试项目中重现该问题:

I am trying to learn about Entity Framework 6, and I am running into an issue, that I have been able to reproduce in a test project:

Movie 有一个Name 和一个Revenue.Revenue 有一个 GrossIncome:

A Movie has a Nameand a Revenue. A Revenue has a GrossIncome:

public class Movie
{
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public Revenue Revenue { get; set; }
}

public class Revenue
{
        [Key]
        public int Id { get; set; }
        public double GrossIncome { get; set; }
}

我正在尝试使用 EF6 代码优先在数据库中保存一些关于电影的数据:

I am trying to use EF6 code-first to persist some data about movies in the database:

public class MovieContext: DbContext
{
    public MovieContext(): base("name=testDB") { }
    public DbSet<Movie> Movies { get; set; }
    public DbSet<Revenue> Revenues { get; set; }
}

我首先在数据库中插入一部新电影,其相关收入:

I start by inserting a new movie, with its associated revenue in the DB:

using (var context = new MovieContext())
{
    Revenue revenue = new Revenue()
                {
                    GrossIncome = 10
                };
    Movie movie = new Movie()
                {
                    Name = "foo",
                    Revenue = revenue
                };

    context.Movies.Add(movie);
    context.SaveChanges();
}

我可以在 SQL Server 中看到创建了表,并且创建了 Movies.Revenue_Id 列,与 Revenue.Id 具有外键关系.

I can see in SQL Server that the tables are created, and that a Movies.Revenue_Id column has been created, with a foreign key relationship to Revenue.Id.

如果我尝试使用 SQL 查询它,它工作正常:

If I try to query it using SQL, it works fine:

SELECT Movies.Name, Revenues.GrossIncome
FROM Movies
LEFT JOIN Revenues ON Movies.Revenue_Id = Revenues.Id

返回

Name    GrossIncome
----------------------
foo         10

但是,如果我尝试使用实体框架来查询数据:

However, if I try to use Entity Framework to query the data:

using (var context = new MovieContext())
{
    List<Movie> movieList = context.Movies.ToList();
    Console.WriteLine("Movie Name: " + movieList[0].Name);

    if (movieList[0].Revenue == null)
    {
        Console.WriteLine("Revenue is null!");
    }
    else
    {
        Console.WriteLine(movieList[0].Revenue.GrossIncome);
    }

    Console.ReadLine();
}

控制台显示:

Movie Name: foo     <- It shows that the query works, and that the data in the main table is fetched.
Revenue is null!    <- Even though the data in the DB is correct, EF does not read the data from the foreign key.

我的问题很简单:我做错了什么?应该如何读取外键值?

My question is simple: what am I doing wrong? How are the foreign key values supposed to be read?

推荐答案

只需包含你要加载的子实体:

Just include the child entity you want to load:

using (var context = new MovieContext())
{
    List<Movie> movieList = context.Movies
                                   .Include(m => m.Revenue)   // ADD THIS INCLUDE
                                   .ToList();
    Console.WriteLine("Movie Name: " + movieList[0].Name);

    if (movieList[0].Revenue == null)
    {
        Console.WriteLine("Revenue is null!");
    }
    else
    {
        Console.WriteLine(movieList[0].Revenue.GrossIncome);
    }

    Console.ReadLine();
}

这将加载电影 - 并确保所有对其各自 .Revenue 引用的引用也已加载.

This will load the movies - and also make sure that all the references to their respective .Revenue references have been loaded, too.

这篇关于如何使用实体框架查询外键对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆