Comments (5)
What if the primary key is not an integer? V2 will support generic args - are we comparing to default(T)
? If so, what then is the behavior when 0 is valid? It's perfectly valid behavior to start a integer primary key identity column at 0 for example:
Create Table MyTable (
Id int Primary Key Identity(0,1) Not Null,
I'm not fundamentally against an Upsert<T, TKey>
(to match the new Insert<T, TKey>
), but I'm not sure what the differentiator would be based on. Unless every provider implements a "try update and if rows = 0 then insert", which also has race semantics in play but is more generally applicable. It'd be nice to skip the shenanigans and just do an insert if we knew that was the case though.
from dapper.contrib.
In my opinon 0, null, or its default value regarding type would be a candidate for insert. But shure I agree it it is not foolproof.
If it is clearly stated along with the method use, I beleve it would make sense to most developers. And also the fact that one could use native insert/update at will.
from dapper.contrib.
In my opinion, it's not about looking for the default(T) key. Instead, the way upsert should work is by looking if an entry already exists with the same primary key before deciding to insert or update.
Of course, if you're intentionally using a table with a valid 0 identity key, you should set your keys to -1 (or other invalid value, or maybe even null) for the new records.
The problem here is about what to do in the query to get such results. The obvious solution in SQL Server is a MERGE, with the table as TARGET and the upsert record as SOURCE. However, if I understand correctly, not all DBs support the MERGE operation, so the query would be specifically created for each possible DB (and maybe a fallback with separate try update and insert operations).
from dapper.contrib.
Any interest in collaborating? I've written an UpsertAsync
method which works for sqlite and mysql/mariadb.
I'd like to use the private helper methods in SqlMapperExtensions.cs
like GetTableName
. Is there a way to access them without having to fork the whole repo?
Could the private methods be re-scoped to public
Inside a separate namespace?
from dapper.contrib.
Here's what I put together for upsert and is more a Contrib-style method: https://github.com/gismofx/DapperUpsert
Would be easy to add Postgres support.
Should I attempt to fork and integrate with Dapper and/or Dapper Contrib?
from dapper.contrib.
Related Issues (20)
- Why a hard reference to the column "id" HOT 5
- IsWriteable not working on Key columns
- System.Data.SqlClient.SqlException: "Cannot insert the value NULL into colum..." (DateTime) HOT 1
- Insert<T> return non-int HOT 2
- Does not work with OleDbConnection HOT 1
- Stupid initiative on plural entity name - REMOVE! HOT 1
- GetAsync fails on SQL Server when the primary key property is a reserved SQL keyword
- InsertAsync and Insert fail if the id is not an int
- Inherited TableAttribute is ignored by CRUD operations
- Snowflake query parameters syntax is not supported HOT 1
- Dapper.Contrib.dll not signed HOT 1
- MySqlAdapter InsertAsync unexpectedly modifies entityToInsert
- Add Bookstore WebAppExample
- Internal Table Properties are Ignored
- No documentation of failure states (or it is hard to find if it exists?)
- Cannot use SqlMapperExentions.Table attribute on structs
- Pervasive/Actian Zen (PSQL)
- Is Dapper.Contrib being actively maintained? HOT 3
- [Question] WriteAtrribute to be inherited
- Usage with EF-related POCOs. Operations fail on navigation properties HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dapper.contrib.