|
| 1 | +# Value Generation |
| 2 | + |
| 3 | +See [the general EF Docs on value generation](https://docs.microsoft.com/en-us/ef/core/modeling/generated-properties) to better understand the concepts described here. |
| 4 | + |
| 5 | +## Serial (Autoincrement) Columns |
| 6 | + |
| 7 | +In PostgreSQL, the standard autoincrement column type is called `serial`. This isn't really a special type like in some other databases (e.g. SQL Server's IDENTITY), but rather a shorthand for specifying that the column's default value should come from a sequence. See [the PostgreSQL docs](https://www.postgresql.org/docs/current/static/datatype-numeric.html#DATATYPE-SERIAL) for more info. |
| 8 | + |
| 9 | +When `ValueGeneratedOnAdd` is specified on a short, int or long property, the Npgsql EF Core provider will automatically map it to a `serial` column. Note that EF Core will automatically recognize key properties by convention (e.g. a property called `Id` in your entity) and will implicitly set them to `ValueGeneratedOnAdd`. |
| 10 | + |
| 11 | +Note that there was a significant and breaking change in 1.1. If you have existing migrations generated with 1.0, please read the [migration notes](migration/1.1.md). |
| 12 | + |
| 13 | +## HiLo Autoincrement Generation |
| 14 | + |
| 15 | +One disadvantage of database-generated values is that these values must be read back from the database after a row is inserted. If you're saving multiple related entities, this means you must perform multiple roundtrips as the first entity's generated key must be read before writing the second one. One solution to this problem is HiLo value generation: rather than relying on the database to generate each and every value, the application "allocates" a range of values, which it can then populate directly on new entities without any additional roundtrips. When the range is exhausted, a new range is allocated. In practical terms, this uses a sequence that increments by some large value (100 by default), allowing the application to insert 100 rows autonomously. |
| 16 | + |
| 17 | +To use HiLo, specify `ForNpgsqlUseSequenceHiLo` on a property in your model's `OnModelCreating`: |
| 18 | + |
| 19 | +```c# |
| 20 | +modelBuilder.Entity<Blog>().Property(b => b.Id).ForNpgsqlUseSequenceHiLo(); |
| 21 | +``` |
| 22 | + |
| 23 | +You can also make your model use HiLo everywhere: |
| 24 | + |
| 25 | +```c# |
| 26 | +modelBuilder.ForNpgsqlUseSequenceHiLo(); |
| 27 | +``` |
| 28 | + |
| 29 | +## Guid/UUID Generation |
| 30 | + |
| 31 | +By default, if you specify `ValueGeneratedOnAdd` on a Guid property, a random Guid value will be generated client-side and sent to the database. |
| 32 | + |
| 33 | +If you prefer to generate values in the database instead, you can do so by specifying `HasDefaultValueSql` on your property. Note that PostgreSQL doesn't include any Guid/UUID generation functions, you must add an extension such as `uuid-ossp` or `pgcrypto`. This can be done by placing the following code in your model's `OnModelCreating`: |
| 34 | + |
| 35 | +```c# |
| 36 | +modelBuilder.HasPostgresExtension("uuid-ossp"); |
| 37 | +modelBuilder |
| 38 | + .Entity<Blog>() |
| 39 | + .Property(e => e.SomeGuidProperty) |
| 40 | + .HasDefaultValueSql("uuid_generate_v4()"); |
| 41 | +``` |
| 42 | + |
| 43 | +See [the PostgreSQL docs on UUID for more details](https://www.postgresql.org/docs/current/static/datatype-uuid.html). |
| 44 | + |
| 45 | +## Computed Columns (On Add or Update) |
| 46 | + |
| 47 | +PostgreSQL does not support computed columns. |
| 48 | + |
0 commit comments