Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net core 3.x Keyless Entity Types avoid table creation

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

keyless entity types

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?

Sample code

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

with .net core 2.2

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

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.

like image 439
Richard Vinoth Avatar asked Feb 05 '20 13:02

Richard Vinoth


People also ask

Can we create table without primary key in Entity Framework Code First?

Popular Answer There is a great difference between what EF can do with a database, and what is possible with a database. Most databases allow for a table to be without a primary key.

Does EF core create database if not exist?

If your database already exists, it can be mapped with the EF Core model. However, if the database doesn't exist already, you would want it to be created. Of course, you can create the database manually by looking at the EF Core model and creating tables accordingly.

What is Efcore?

Entity Framework (EF) Core is a lightweight, extensible, open source and cross-platform version of the popular Entity Framework data access technology. EF Core can serve as an object-relational mapper (O/RM), which: Enables . NET developers to work with a database using . NET objects.

What are keyless entity types in efcore?

In EF Core 3.0 the concept was renamed to keyless entity types. The [Keyless]Data Annotation became available in EFCore 5.0. In addition to regular entity types, an EF Core model can contain keyless entity types, which can be used to carry out database queries against data that doesn't contain key values.

What is [keyless]data annotation in EF Core?

The [Keyless]Data Annotation became available in EFCore 5.0. In addition to regular entity types, an EF Core model can contain keyless entity types, which can be used to carry out database queries against data that doesn't contain key values. Defining Keyless entity types

What is owned entity type in EF Core?

Owned Entity Types. This feature is new in EF Core 2.0. EF Core allows you to model entity types that can only ever appear on navigation properties of other entity types. These are called owned entity types. The entity containing an owned entity type is its owner.

What's new in Entity Framework Core 3x?

New features in Entity Framework Core 3.x 1 LINQ overhaul. ... 2 Cosmos DB support. ... 3 C# 8.0 support. ... 4 Interception of database operations. ... 5 Reverse engineering of database views. ... 6 Dependent entities sharing the table with the principal are now optional. ... 7 EF 6.3 on .NET Core. ... 8 Postponed features. ...


Video Answer


1 Answers

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.

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.

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

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

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

modelBuilder.Entity<CustomQuery>().HasNoKey().ToView(null);
like image 153
Ivan Stoev Avatar answered Oct 21 '22 16:10

Ivan Stoev