0

How do i change userid using Guid every time when the user want to insert files to database to prevent the violation of primary key (duplication of primary key), userid is the primary key?

public System.Guid userid { get; set; } Its producing this 00000000-0000-0000-0000-000000000000 output

Anesu
  • 9
  • 1
  • 6

2 Answers2

1

You do not prevent it. The DBMS and only the DBMS is responsible to creating the Primary Key values and maintaining it's constraints. Doing this in your Client is just going to fail horribly. You will run head first into a race condition that way.

If you do need the Key of something you just inserted, SQL actually has the OUTPUT Syntax. I remember other DBMS not having something comparable, however. But usually you can make something using a stored funciton equivalent and a wide enough transaction/lock.

As a side note, it is worth mentioning that there are 3 ways to save BLOBS with/in DBs. I can not really tell wich of the 3 you are using.

Christopher
  • 9,634
  • 2
  • 17
  • 31
0

You just need to set the userid value

userid = Guid.NewGuid();
Joe
  • 349
  • 2
  • 3