Question

It's not completely clear to me what's the diference between using AsNoTracking() on a entity that has a key, and using HasNoKey(). As I understand, when using AsNoTracking(), even though the changetracker does not track changes on the resulting objects, the objects are still kept in the DbContext's memory. If you try to Attach a new object with the same key as one already in memory, you get an error. (correct me if i'm wrong).

So is the behavior the same if you use HasNoKey (former DbQuery<>)?

1
1
9/28/2019 3:11:07 PM

Popular Answer

Introducing ModelBuilder.Entity<>().HasNoKey() is one of the breaking changes in EF Core 3.0 as stated:

A query type now becomes just an entity type without a primary key. Keyless entity types have the same functionality as query types in previous versions.

Query types were a means to query data that doesn't define a primary key in a structured way. That is, a query type was used for mapping entity types without keys (more likely from a view, but possibly from a table) while a regular entity type was used when a key was available (more likely from a table, but possibly from a view).

You said that:

If you try to Attach a new object with the same key as one already in memory, you get an error. (correct me if i'm wrong). So is the behavior the same if you use HasNoKey (former DbQuery<>)?

-Tracking in EF Core transaction are based on Entity Primary key. As the above documentation clearly stating that EntityType with .HasNoKey() is QueryType which does not have any key defined. So tracking is completely void in case of EntityType with HasNoKey().

For more details : Query types are consolidated with entity types

1
9/28/2019 5:27:35 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow