是否可以在Entity Framework Core 2.1版上自动映射数据库视图? [英] Is it possible to automatically map a DB view on Entity Framework Core version 2.1?

查看:73
本文介绍了是否可以在Entity Framework Core 2.1版上自动映射数据库视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现关于stackoverflow的这个有趣的问题有关映射视图并在EF Core中使用它.
根据其答案,以前的EF Core版本无法自动映射视图.

I found this interesting question on stackoverflow about mapping a view and working with it in EF Core.
According to its answer, with previous EF Core versions wasn't possible to automatically map views.

但是现在呢?根据实体框架2.1路线图

现在,EF Core模型可以包含查询类型.与实体类型不同,
查询类型没有定义键,因此无法插入,
删除或更新(即它们是只读的),但可以将其返回
直接通过查询.查询类型的一些使用场景是:

An EF Core model can now include query types. Unlike entity types,
query types do not have keys defined on them and cannot be inserted,
deleted or updated (i.e. they are read-only), but they can be returned
directly by queries. Some of the usage scenarios for query types are:

  • 映射到没有主键的视图
  • (...)

所以问题是:是否可以自动搭建数据库上下文并映射其视图(就像我们对

So the question is: is it possible to automatically scaffold a db context and map its views (like we do for a normal scaffold-dbcontext with an existing database)? If yes, does anyone know how?

还是唯一的方法仍然是手动创建,如Sampath Kaliyamurthy在此答案中对以前的EF Core版本所说的那样?

Or the only way is still creating them manually as Sampath Kaliyamurthy said in this answer for a previous EF Core version?

推荐答案

可以 折叠视图.只需使用-Tables来构建表的方式,仅使用视图的名称即可.例如,如果您的视图名称为"vw_inventory",则在Package Manager控制台中运行此命令(用您自己的信息替换"My ..."):

It is possible to scaffold a view. Just use -Tables the way you would to scaffold a table, only use the name of your view. E.g., If the name of your view is ‘vw_inventory’, then run this command in the Package Manager Console (substituting your own information for "My..."):

PM> Scaffold-DbContext "Server=MyServer;Database=MyDatabase;user id=MyUserId;password=MyPassword" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Temp -Tables vw_inventory

此命令将在项目的Temp目录中创建模型文件和上下文文件.您可以将模型文件移至您的模型目录中(请记住要更改名称空间名称).您可以从上下文文件中复制所需的内容,然后将其粘贴到项目中相应的现有上下文文件中.

This command will create a model file and context file in the Temp directory of your project. You can move the model file into your models directory (remember to change the namespace name). You can copy what you need from the context file and paste it into the appropriate existing context file in your project.

注意:如果要在使用本地数据库的集成测试中使用视图,则需要在数据库设置中创建视图.如果您打算在多个测试中使用该视图,请确保添加检查该视图是否存在.在这种情况下,由于必须将SQL创建视图"语句作为批处理中的唯一语句,因此您需要在存在检查语句中将创建视图作为动态Sql运行.或者,您可以运行单独的如果存在放置视图...",然后运行创建视图"语句,但是如果同时运行多个测试,则不希望在使用另一个测试的情况下删除该视图.示例:

Note: If you want to use your view in an integration test using a local db, you'll need to create the view as part of your db setup. If you’re going to use the view in more than one test, make sure to add a check for existence of the view. In this case, since the SQL ‘Create View’ statement is required to be the only statement in the batch, you’ll need to run the create view as dynamic Sql within the existence check statement. Alternatively you could run separate ‘if exists drop view…’, then ‘create view’ statements, but if multiple tests are running concurrently, you don’t want the view to be dropped if another test is using it. Example:

  void setupDb() {
    ...
    SomeDb.Command(db => db.Database.ExecuteSqlRaw(CreateInventoryView()));
    ...
  }
  public string CreateInventoryView() => @"
  IF OBJECT_ID('[dbo].[vw_inventory]') IS NULL
    BEGIN EXEC('CREATE VIEW [dbo].[vw_inventory] AS
       SELECT ...')
    END";

这是一个有用的链接.它描述了手动添加代码段而不是对其进行脚手架添加: https://docs.microsoft.com/zh-cn/ef/core/modeling/keyless-entity-types?tabs=fluent-api

This is a helpful link. It describes adding the code sections by hand instead of scaffolding them: https://docs.microsoft.com/en-us/ef/core/modeling/keyless-entity-types?tabs=fluent-api

这篇关于是否可以在Entity Framework Core 2.1版上自动映射数据库视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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