abp icon indicating copy to clipboard operation
abp copied to clipboard

Questions about uniqueidentifier as primary key

Open aklscv opened this issue 6 years ago • 4 comments

I noticed that ABP uses uniqueidentifier as the default primary key type in SQL Server. I will use the same for the entities/tables that I will be creating so I don't have half using uniqueidentifier and half using some other type like long. I read that we should be using the IGuidGenerator to generate the IDs for our entities so it generates sequentially which is a cool feature. Given this context I have two questions:

1 - When seeding data can/should I have a hard-coded GUID for the ID instead of using IGuidGenerator? The reason for this is so my seed data in production doesn't have a different id from my seed data in other environments. Would this create any problems/concerns?

2 - How do I create data directly in the database (insert) without application code? I believe I can use newid() in SQL Server but would this create any problems?

I am looking for the best approach around the ABP framework.

aklscv avatar Apr 08 '20 23:04 aklscv

Hi @aklscv

  1. If you want the GUID of the seed data for different environments to be the same, you can hard-code it, but make sure that the GUID is unique.
  2. If you don't explicitly use IGuidGenerator, and the Id value is not set, the ABP framework will use it internally. https://github.com/abpframework/abp/blob/0ee3e328a5c8beb40e426318f82932a19eb7dd68/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs#L403

See the entity documentation for best practices. ABP has many pre-built modules, you can also use it as a reference

realLiangshiwei avatar Apr 09 '20 00:04 realLiangshiwei

@liangshiw Thanks for the reply that clarifies things a bit but I think the 2nd question was actually about a SQL INSERT statement directly to the database.

gvassao avatar Apr 09 '20 01:04 gvassao

@gvassao If you use SQL, then you should follow the SQL specification. This has nothing to do with ABP, however you should make GUID sequential ,this is critical to performance. See https://docs.microsoft.com/en-us/sql/t-sql/data-types/uniqueidentifier-transact-sql?view=sql-server-ver15

realLiangshiwei avatar Apr 09 '20 01:04 realLiangshiwei

@liangshiw Yeah that is what I thought. I just wanted to confirm that ABP had nothing built-in to deal with this situation. Thanks!

@aklscv Think you can close it

gvassao avatar Apr 09 '20 01:04 gvassao