c# - Detect if a new record is the same as the existing record in database -


suppose there records in database this:

id v1 v2 v3
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4

and suppose new records this:
id v1 v2 v3
1 1 2 1
5 5 5 5

now, want insert new records in way:
1. check if id exists in database;
2. if doesn't exist, insert record directly;
3. if exists, detect if record same new one. if not, update changed values.

since real data large , contains 50+ variables , want make inserting fast, ask there general way detect if new record of same id different existing record in entity framework. can't 50+ if see variables have been changed.

please make sure solution applicable large data set, thanks.

you can use merge perform "upsert" operation:

merge #t tgt using ( values     (1,1,2,1),     (5,5,5,5) ) src (id, v1, v2, v3) on tgt.id = src.id when matched     update set tgt.v1 = src.v1,                tgt.v2 = src.v2,                tgt.v3 = src.v3 when not matched target     insert (id, v1, v2, v3)     values (src.id,             src.v1,             src.v2,             src.v3); 

working example

bol: https://msdn.microsoft.com/en-us/library/bb510625.aspx


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -