@@ -10,13 +10,11 @@ features.
10101 . ** Row-Level Locking** : Implements the PostgreSQL ` FOR UPDATE ` feature, providing three lock
1111 behaviors - ` Wait ` , ` Skip ` , and
1212 ` NoWait ` , to facilitate advanced transaction control and concurrency management.
13- 2 . ** Npgsql COPY Integration (Obsolete)** : Offers a high-performance, typed interface for the PostgreSQL COPY command,
14- allowing for
15- bulk data operations within the EF Core framework. This feature significantly enhances data insertion speeds and
16- efficiency.
17- 3 . ** Random Incrementing Sequence Generation:** Provides a secure way to generate sequential IDs with random increments
13+ 2 . ** Random Incrementing Sequence Generation:** Provides a secure way to generate sequential IDs with random increments
1814 to prevent predictability and potential data exposure. This ensures IDs are non-sequential and non-predictable,
1915 enhancing security and balancing database load.
16+ 3 . ** Natural Sorting** : Provides way to calculate natural sort compliant order for string, which can be used
17+ in ` ORDER BY ` clause. This is useful for sorting strings that contain numbers in a human-friendly way.
2018
2119## Installation
2220
@@ -123,59 +121,40 @@ public partial class PgFunction : Migration
123121 enhancing security.
124122- The feature supports only ` long ` data type (` bigint ` in PostgreSQL).
125123
126- ### Npgsql COPY Integration (Obsolete: Use EFCore.BulkExtensions.PostgreSql instead)
124+ ### Natural Sort Key
127125
128- For bulk data operations, use the ` BulkInsert ` or ` BulkInsertAsync ` extension methods:
126+ This package can generate a natural sort key for your text columns—especially useful when sorting addresses or other
127+ fields that contain embedded numbers. It avoids plain lexicographic ordering (e.g. ` "10" ` < ` "2" ` ) by treating numeric
128+ substrings numerically.
129129
130- ``` csharp
131- public async Task BulkInsertExampleAsync ()
132- {
133- var users = new List < UserEntity >();
134- for ( int i = 0 ; i < 10000 ; i ++ )
130+ #### How to Use
131+
132+ 1 . Create the function in your migration (once per database). Call the helper method in ` Up() ` :
133+ ``` csharp
134+ public partial class AddNaturalSortKeyToBuildings : Migration
135135 {
136- users .Add (new UserEntity { /* Initialization */ });
136+ protected override void Up (MigrationBuilder migrationBuilder )
137+ {
138+ // Create the natural sort key function in PostgreSQL
139+ migrationBuilder .CreateNaturalSortKeyFunction ();
140+
141+ }
137142 }
138-
139- await dbContext .Users .BulkInsertAsync (users ); // Or use BulkInsert for synchronous operation
140- // It also saves changes to the database
141- }
142- ```
143-
144- #### Benchmarks
145-
146- The integration of the Npgsql COPY command showcases significant performance improvements compared to traditional EF
147- Core and Dapper methods:
148-
149- ##### General Benchmark Results
150-
151- | Caption | Big O Notation | 1M Rows | Batch Size |
152- | ------------| ----------------| -------------| ------------|
153- | BulkInsert | O(log n) | 350.000 r/s | No batch |
154- | Dapper | O(n) | 20.000 r/s | 1500 |
155- | EFCore | O(n) | 10.600 r/s | 1500 |
156-
157- ##### Detailed Benchmark Results
158-
159- | Operation | BulkInsert | Dapper | EF Core |
160- | -------------| ------------| --------| ---------|
161- | Insert 10K | 76ms | 535ms | 884ms |
162- | Insert 100K | 405ms | 5.47s | 8.58s |
163- | Insert 1M | 2.87s | 55.85s | 94.57s |
164-
165- ##### Efficiency Comparison
166-
167- | RowsCount | BulkInsert Efficiency | Dapper Efficiency |
168- | -----------| ----------------------------| ---------------------------|
169- | 10K | 11.63x faster than EF Core | 1.65x faster than EF Core |
170- | 100K | 21.17x faster than EF Core | 1.57x faster than EF Core |
171- | 1M | 32.95x faster than EF Core | 1.69x faster than EF Core |
172-
173- ##### Additional Notes
174-
175- - The ` BulkInsert ` feature currently does not support entity properties intended for ` JSON ` storage.
176-
177- - The performance metrics provided above are based on benchmarks conducted under controlled conditions. Real-world
178- performance may vary based on specific use cases and configurations.
143+ ```
144+ 2 . Configure your entity to use the natural sort key . In your `IEntityTypeConfiguration ` for the table:
145+ ```csharp
146+ public class BuildingConfiguration : IEntityTypeConfiguration<Building>
147+ {
148+ public void Configure(EntityTypeBuilder <Building > builder )
149+ {
150+ // Create a computed column in EF (like "address_natural_sort_key")
151+ builder
152+ .Property (x => x .AddressNaturalSortKey )
153+ .HasNaturalSortKey (" address" ); // Points to the column storing your original address
154+ }
155+ }
156+ ```
157+ When you query the entity , simply `ORDER BY AddressNaturalSortKey ` to get true “natural ” ordering in PostgreSQL .
179158
180159## License
181160
0 commit comments