This article is a mirror article of machine translation, please click here to jump to the original article.

View: 30236|Reply: 1

[Source] EF sets the primary key Id to the guid self-growing type

[Copy link]
Posted on 11/4/2016 1:27:38 PM | | | |


The entity entity code is as follows:



The insert code is as follows:



We only set the value of name and do not assign a value to id, let's take a look at what the generated sql statement is??? , as follows:

exec sp_executesql N'DECLARE @generated_keys table([Id] uniqueidentifier)
INSERT [dbo]. [TestTab] ([Name])
OUTPUT inserted. [Id] INTO @generated_keys
VALUES (@0)
SELECT t.[Id]
FROM @generated_keys AS g JOIN [dbo]. [TestTab] AS t ON g.[Id] = t.[Id]
WHERE @@ROWCOUNT > 0',N'@0 nvarchar(32)', @0=N' architectwww.itsvse.com'


We found that the INSERT TestTab table only inserts the Name field, does Id have a default value??? We found that

ef has helped me set the default value type of the Id primary key, the default value or binding "(newsequentialid())", and used the newsequentialid() function, which is introduced as follows:


1.:The biggest advantage of the newsequentialid function over the newid function is that if you create an index on a UNIQUEIDENTIFIER field, the new value generated using newid is not fixed, so the new value causes the change in the index B+ tree to be random. If the new value produced by the newsequentialid is regular, then the change of the index B+ tree is regular. Regularity and irregularity lead to performance improvements.

2: UNIQUEIDENTIFIER is a very convenient thing to do as the primary key, which has irreplaceable advantages in operations such as data merging
However, due to the decentralization of ordinary GUIDs, if the primary key is added to a clustered index, it will be much less efficient when inserting records

SQL SERVER 2005 adds a new function called NEWSEQUENTIALID, which MSDN explains:
Create a GUID on the specified machine that is larger than any GUID previously generated by the function.
NEWSEQUENTIALID() cannot be referenced in the query.
Note: This means that it can only be used as the DEFAULT VALUE of a database column, and cannot execute statements like SELECT NEWSEQUENTIALID().
The GUID generated by NEWSEQUENTIALID() is unique in that particular computer only if the computer does not have a network card.
Note: This sentence is wrong, it should be that only when the computer has a network card, the generated GUID is unique in the world
You can use NEWSEQUENTIALID() to generate a GUID to reduce page contention on the leaf-level index.





Previous:EasyUI Control Tab adds a toggle function
Next:c#/.Net collection array layer by layer value
Posted on 2/1/2018 10:43:26 AM |
Yes, thank you! The landlord's skills are very hard! Hard
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com