Check on primary key field when inserting object #162

Closed
wants to merge 1 commit into
from

Projects

None yet

2 participants

@mountain65

Since we assume an identity column anyway (we check for @@IDENTITY after insert) let's assume that the identity column is the same as the primary key.
If I insert a POCO the primary key column is there and thus the column is added to the SQL insert statement. SQL won't allow that gving a "set identity_insert is off for table 'xxx'. Proposed change filters the primary key column out from the list of properties.

Richard de Zwart Check on primary key field when inserting object
Since we assume an identity column anyway (we check for @@IDENTITY after insert)
let's assume that the identity column is the same as the primary key. If I insert 
a POCO the primary key column is there and thus the column is added to
the SQL insert statement. Proposed change filters the primary key column out from
the list of properties.
9f162ab
@robconery
Collaborator

Is this a problem you've faced? If the value of the column isn't set, it isn't added. If it is set, it's not an update :). I'm curious what your use case is here?

Also, Save() checks for this exact thing:
https://github.com/robconery/massive/blob/master/Massive.cs#L246

@mountain65

I think the problem comes from the fact that I pass in a Poco, not a dynamic. In that case the primary key field of my object always has the default value of 0, so it is actually set. If I call Save() I get an update on a non-existing record, if I call Insert() it raises the above problem.
Yep, I've heard you say before: why not switch to dynamics? ;-)

@robconery
Collaborator

Unfortunately defaulting to 0 is a problem here - that's a value and it's invalid as far as your DB is concerned. This is the difference between a query tool and an ORM :). This is not a change I'm wanting to make.

@robconery robconery closed this Dec 23, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment