Posts

Showing posts with the label database

Using GUIDs with EF Core

Introduction GUIDs , also known as UUIDs , are a very useful data type that is present in all major databases. It allows having values that are magically generated and never repeat, making them ideal for usage across data sources, where a single source of data cannot be reused. in .NET, they are represented by the Guid type; in SQL Server, it is UNIQUEIDENTIFIER , in PostgreSQL it is UUID , in Oracle it is RAW(16) , and in MySQL it is BINARY(16) . EF Core supports all of them. There are pros and cons regarding using GUIDs as database primary keys: They are obviously great for uniqueness They can be generated on the client But, on the other side: They take a lot of space (16 bytes), compared to 4 bytes for integers or 8 for longs They can lead to very fragmented indexes Let's see what we can do about that. Primary Key Generation in EF Core There are 3 ways to have EF Core generate GUIDs for the primary keys: Have the primary key property marked as database generated of type identit...

Text Querying with EF Core and SQL Server

Introduction SQL Server provides a number of functions that can be used for non-exact text queries over text columns ( CHAR , NCHAR , VARCHAR , NVARCHAR , TEXT , NTEXT ). These include searching a piece of text contained inside another, using full text search, or using patterns, and computing a code from a text. Some of them are: CONTAINS : Searches for precise or fuzzy (less precise) matches to single words and phrases, words within a certain distance of one another, or weighted matches in SQL Server DIFFERENCE : This function returns an integer value measuring the difference between the  SOUNDEX  values of two different character expressions FREETEXT : Is a predicate used in the Transact-SQL WHERE clause of a Transact-SQL SELECT statement to perform a SQL Server full-text search on full-text indexed columns containing character-based data types LIKE : Determines whether a specific character string matches a specified pattern PATINDEX : Returns the startin...