简体   繁体   English

EF7中的多对多关系-选择查询以获取包含相关数据的列表

[英]Many-to-Many Relationships in EF7 - Select query for a list including related data

I am building a blog with the following Many-to-Many relationship on the content -> category tables. 我正在建立一个在内容->类别表上具有以下多对多关系的博客。 I have used the following code to generate the tables in EF7: 我使用以下代码在EF7中生成表:

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }

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

public class Categorization
{
    public int ContentId { get; set; }
    public Content Content { get; set; }

    public int CategoryId { get; set; }
    public Category Category { get; set; }
}

public class Content
{
    [Key]
    public int Id { get; set; }
    public string Title { get; set; }
    public string Excerpt { get; set; }
    public string Body { get; set; }

    ...

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

However, when I attempt to load a list of categories, including the content associated with an EF7 query as follows, I get an error. 但是,当我尝试加载类别列表时,包括如下所示的与EF7查询相关的内容,我收到了错误消息。

categories = await _db.Categories
                      .Include(c => c.Categorization)
                      .ThenInclude(c => c.Content)
                      .OrderByDescending(c => c.Categorization.Count)
                      .ToListAsync();

This is the error page that I get when this is called. 这是调用此方法时得到的错误页面。

A database operation failed while processing the request. 处理请求时,数据库操作失败。

AggregateException: One or more errors occurred. AggregateException:发生一个或多个错误。 AggregateException: One or more errors occurred. AggregateException:发生一个或多个错误。 AggregateException: One or more errors occurred. AggregateException:发生一个或多个错误。 AggregateException: One or more errors occurred. AggregateException:发生一个或多个错误。 SqlException: Invalid column name 'Id'. SqlException:无效的列名称'Id'。 There are pending model changes for ApplicationDbContext Scaffold a new migration for these changes and apply them to the database from the command line: 对于ApplicationDbContext Scaffold,有待处理的模型更改,这些更改将进行新的迁移,并从命令行将其应用于数据库:

 dnx ef migrations add [migration name] dnx ef database update 

Interesting note: When i remove the line 有趣的注意:当我删除行时

 .Include(c => c.Categorization).ThenInclude(c => c.Content)

It will sometimes work correctly, as in load the categorization, however this does not ALWAYS happen, sometimes the categorization doesn't load and I only have null references for Categorization in each Category class in the generated enumerable. 它有时会正常工作,就像在加载分类中一样,但是这种情况始终不会发生,有时分类不会加载,并且在生成的枚举中的每个Category类中,我对分类的引用都为空。

The error message says it all. 错误消息说明了一切。 SqlException: Invalid column name 'Id' SqlException:无效的列名称'Id'

Your database is different than the models on your project. 您的数据库与项目上的模型不同。 If you have database migration enabled then run the commands 如果启用了数据库迁移,请运行以下命令

dnx ef migrations add [migration name] 
dnx ef database update

If you don't want to use database migration, you have to update the models on your database to match your models. 如果您不想使用数据库迁移,则必须更新数据库上的模型以匹配您的模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM