在Entity Framework Core中使用SQL视图

问题描述 投票:30回答:5

例如,我有这样的模型:

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public BlogImage BlogImage { get; set; }
}

public class BlogImage
{
    public int BlogImageId { get; set; }
    public byte[] Image { get; set; }
    public string Caption { get; set; }

    public int BlogId { get; set; }
    public Blog Blog { get; set; }
} 

我想在ImageView视图中返回Url和Image。

我在哪里需要创建和定义SQL视图?

c# entity-framework entity-framework-core
5个回答
43
投票

在Entity Framework Core 2.1中,我们可以使用Query Types作为Yuriy N建议的。

有关如何使用它们的更详细的文章可以找到here

根据文章的例子,最直接的方法是:

1.我们有以下实体模型来管理出版物

public class Magazine
{
  public int MagazineId { get; set; }
  public string Name { get; set; }
  public string Publisher { get; set; }
  public List<Article> Articles { get; set; }
}

public class Article
{
  public int ArticleId { get; set; }
  public string Title { get; set; }
  public int MagazineId { get; set; }
  public DateTime PublishDate { get;  set; }
  public Author Author { get; set; }
  public int AuthorId { get; set; }
}
public class Author
{
  public int AuthorId { get; set; }
  public string Name { get; set; }
  public List<Article> Articles { get; set; }
}

2.我们有一个名为AuthorArticleCounts的视图,定义为返回作者编写的文章的名称和数量

SELECT
  a.AuthorName,
  Count(r.ArticleId) as ArticleCount
from Authors a
  JOIN Articles r on r.AuthorId = a.AuthorId
GROUP BY a.AuthorName

3.我们去创建一个用于View的模型

public class AuthorArticleCount
{
  public string AuthorName { get; private set; }
  public int ArticleCount { get; private set; }
}

4.我们在DbContext中创建一个DbQuery属性来使用Model中的视图结果

public DbQuery<AuthorArticleCount> AuthorArticleCounts{get;set;}

5.最后,我们可以轻松地获得View的结果。

var results=_context.AuthorArticleCounts.ToList();

22
投票

Entity Framework Core目前不支持视图。见https://github.com/aspnet/EntityFramework/issues/827

也就是说,您可以通过将实体映射到视图来欺骗EF使用视图,就像它是一个表一样。这种方法有局限性。例如如果您无法使用迁移,则需要为我们手动指定EF的密钥,并且某些查询可能无法正常运行。要绕过最后一部分,您可以手动编写SQL查询

context.Images.FromSql("SELECT * FROM dbo.ImageView")

13
投票

以下是在EF Core中使用SQL视图的新方法:Query Types


10
投票

EF Core不会在上下文calss中自动为SQL视图创建DBset,我们可以手动添加它们,如下所示。

public partial class LocalDBContext : DbContext
{ 

    public LocalDBContext(DbContextOptions<LocalDBContext> options) : base(options)
    {

    }

    public virtual DbSet<YourView> YourView { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<YourView>(entity => {
            entity.HasKey(e => e.ID);
            entity.ToTable("YourView");
            entity.Property(e => e.Name).HasMaxLength(50);
        });
    }

}

示例视图定义如下,几个属性

using System;
using System.Collections.Generic;

namespace Project.Entities
{
    public partial class YourView
    {
        public string Name { get; set; }
        public int ID { get; set; }
    }
}

在上下文类中为视图和数据库集添加类后,最好通过控制器中的上下文对象使用视图对象。


1
投票

QueryTypes是EF Core 2.1的规范答案,但是从数据库第一种方法迁移时我已经使用了另一种方法(该视图已在数据库中创建):

  • 定义模型以匹配视图列(匹配模型类名称以查看名称或使用表属性。确保[Key]属性应用于至少一列,否则数据获取将失败
  • 在您的上下文中添加DbSet
  • 添加迁移(添加迁移)
  • 根据提供的模型删除或注释掉用于创建/删除要创建/删除的“表”的代码
  • 更新数据库(更新数据库)
© www.soinside.com 2019 - 2024. All rights reserved.