.Net core 3.x Keyless Entity Types 避免表创建 [英] .Net core 3.x Keyless Entity Types avoid table creation

查看:15
本文介绍了.Net core 3.x Keyless Entity Types 避免表创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在实体框架核心 3.1.1 中执行一个复杂的 sql 查询,在研究中我发现无键实体类型是代码优先方法的方法.我看到很多关于 dbquery 的文档,但这在 .net core 3.x 中被标记为过时

I need to execute a complex sql query in entity framework core 3.1.1, on researching i found out that keyless entity types is the way to go in code first approach. I see lot of documents for dbquery but this is marked as obsolete in .net core 3.x

无键实体类型

根据 Microsoft 文档,它说 dbquery 已过时,因此请改用 dbset 方法,但使用 dbset 它试图在数据库中创建一个新表.如何在应用迁移时禁用无键实体类型中的表生成?

As per Microsoft documentation it says dbquery is obsolete so use dbset approach instead, but with dbset it is trying to create a new table in database. how to disable table generation in keyless entity types while applying migrations?

示例代码

public class ApplicationContext : DbContext
{
 public DbSet<CustomQuery> CustomQuery { get; set; }
 protected override void OnModelCreating(ModelBuilder modelBuilder)
 {
    modelBuilder.Ignore<CustomQuery>();
    modelBuilder.Entity<CustomQuery>().HasNoKey();
 }
}

使用 .net 核心 2.2

with .net core 2.2

var entity = _context.Query<CustomQuery>().FromSqlRaw(Regex.Unescape(selectQuery)).AsNoTracking().FirstOrDefault();

使用 .net 核心 3.1

with .net core 3.1

var newEntity = _context.CustomQuery.FromSqlRaw(Regex.Unescape(selectQuery)).AsNoTracking().FirstOrDefault();

如果我尝试应用迁移,则会以自定义查询的名义创建一个新表,但我不需要这种情况发生.因为这只是用于保存连接查询中的值的模型,我不会插入、更新或删除此表中的值.如何实现这一目标?

if i try to apply migrations then a new table in the name of custom query is being created, but I don't need this to happen. because this is just an model used to hold the values from the join query and i will not insert, update or delete the values in this table. how to achieve this?

或者对于这种情况有没有更好的方法.

or is there any better approach for this situation.

推荐答案

这是 EF Core 3 中的一个已知缺陷,在此处报告 3.0 升级 - 带有 HasNoKey()(以前是查询类型)的实体在添加迁移时尝试创建表 #18116.

This is a known defect in EF Core 3, reported here 3.0 Upgrade - Entity with HasNoKey() (formerly a query type) tries to create tables when adding migration #18116.

作为 To vs From 方法的重复"关闭:建议合理化 ToTable、ToQuery、ToView,FromSql 和其他相关方法 #17270能够排除/跳过/忽略部分迁移模型,以便不创建表(对于重叠的有界上下文)#2725,两者都计划在 5.0 版本中发布,这意味着它最终会在该版本中得到解决.

Closed as "duplicate" of To vs From methods: Proposal to rationalize ToTable, ToQuery, ToView, FromSql, and other related methods #17270 and Ability to exclude/skip/ignore parts of the model from migrations so that a table is not created (for overlapping bounded contexts) #2725, both scheduled for 5.0 release, which means it would eventually be addressed in that release.

当前的解决方法在评论中由其中一位提到EF 核心团队成员:

The current workaround is mentioned in the comments by one of the EF Core team members:

现在,您可以使用类似 .ToView("You forgot to use FromSql with ModQueueEntry")

For now, you can just use something like .ToView("You forgot to use FromSql with ModQueueEntry")

或更一般地,使用 .ToView(null),例如

or more generally, using .ToView(null), e.g.

modelBuilder.Entity<CustomQuery>().HasNoKey().ToView(null);

这篇关于.Net core 3.x Keyless Entity Types 避免表创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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