Questions about uniqueidentifier as primary key
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.
Hi @aklscv
- 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.
- 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
@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 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
@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