efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Support insertion of keyless entities

Open ronnyek opened this issue 4 years ago • 6 comments

I've got a case where I'm writing time series data to a postgresql database. In mapping an Entity that represents a record in a time series table, I've explicitly and purposely forgone an ID field, because it'd likely overflow, and wont be used for anything in the future. (there are indexes there to help with querying, but they are specific to postgres/timescaledb).

I've marked the entity with [Keyless] and understand why the entity can't be tracked, but I am confused by why entity framework couldn't handle deletes and explicit inserts, and just fail to do an update where necessary. Can I turn off tracking for a specific DbSet or something, be able to take advantage of querying, inserts, and deletes?

Is there anyway to work around it and be able to write data with no id column to the the db, without having to manually write a bunch of parameterized insert statements?

Is there any likelihood of this being changed or improved in the future?

ronnyek avatar Apr 26 '21 20:04 ronnyek

I am confused by why entity framework couldn't handle deletes

What do you suggest EF send for the delete if there's no primary key? How would the datbase row(s) be identified in the SQL DELETE command? There's very little difference between DELETE and UPDATE - both require the concept of identity to define which row(s) are being targeted. Note that when bulk updates are implemented (#795), you'd be able to express arbitrary DELETE/UPDATE operations, including over keyless entity types.

EF could indeed support inserting keyless entities, though - not sure whether we already have an issue tracking that.

roji avatar Apr 26 '21 22:04 roji

Yeah .. I realize deleting a record by Id would require an Id

ronnyek avatar Apr 26 '21 22:04 ronnyek

Very related to #9118 (Do not track after SaveChanges()), maybe best to do them together.

roji avatar Dec 19 '21 15:12 roji

It's also related to https://github.com/dotnet/efcore/issues/795, specifically the syntax used to insert data from one table to another could also be used to insert arbitrary data without it being tracked.

AndriySvyryd avatar Jan 08 '22 03:01 AndriySvyryd

Working on a completely separate project, totally different needs and I find myself needing this exact same thing again. Writing timeseries data that I can map and query as keyless, but cant insert. In my case I'm using npgsql and timescaledb, and timescale handles archiving/aggregation building etc. The closest thing to a key that I'd want would be the timestamp metric.

I get how keyless insert could be problematic with tracking and updates would be even more difficult, I don't feel like I should have to revert to totally manual queries for writing... because efcore knows how to do this stuff already.

Inserts and tracking I get, but my specific use case I wont be fetching a record, and updating it. Infact I will be unlikely to ever update these keyless records.

So now I have to basically create/add an id column that I will never use taking up space...

ronnyek avatar Jan 25 '23 15:01 ronnyek

@ronnyek this is definitely something we intend to support.

Removing from the backlog as this is basically a dup of #29897.

roji avatar Jan 25 '23 16:01 roji