You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -89,7 +89,7 @@ using var context = dbContextFactory.CreateDbContext();
89
89
90
90
When using ASP.NET - or any application with dependency injection - the context instance will be injected into your code. Use the following to configure EF with your DI container:
If you're using a version of EF prior to 9.0, the above configuration methods aren't available. You can still create an `NpgsqlDataSource` yourself, and then pass it EF's `UseNpgsql()`:
@@ -27,7 +27,7 @@ This configures all aspects of Npgsql to use your `Mood` enum - both at the EF a
27
27
28
28
If you're creating an external NpgsqlDataSource and passing it to `UseNpgsql`, you must make sure to map your enum on that data independently of the EF-level setup:
Since version 7.0, NpgsqlDataSource is the recommended way to use Npgsql. However, if you're not yet using NpgsqlDataSource, map enums by adding the following code, *before* any EF Core operations take place. An appropriate place for this is in the static constructor on your DbContext class:
@@ -91,7 +91,7 @@ This code lets Npgsql know that your CLR enum type, `Mood`, should be mapped to
91
91
92
92
Once your enum is properly set up with EF, you can use your CLR enum type just like any other property:
93
93
94
-
```c#
94
+
```csharp
95
95
publicclassBlog
96
96
{
97
97
publicintId { get; set; }
@@ -113,7 +113,7 @@ using (var ctx = new MyDbContext())
113
113
114
114
The Npgsql provider only allow adding new values to existing enums, and the appropriate migrations will be automatically created as you add values to your CLR enum type. However, PostgreSQL itself doesn't support removing enum values (since these may be in use), and while renaming values is supported, it isn't automatically done by the provider to avoid using unreliable detection heuristics. Renaming an enum value can be done by including [raw SQL](https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/managing?tabs=dotnet-core-cli#arbitrary-changes-via-raw-sql) in your migrations as follows:
115
115
116
-
```c#
116
+
```csharp
117
117
migrationBuilder.Sql("ALTER TYPE mood RENAME VALUE 'happy' TO 'thrilled';");
Copy file name to clipboardExpand all lines: conceptual/EFCore.PG/mapping/full-text-search.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ PostgreSQL has [built-in support for full-text search](https://www.postgresql.or
6
6
7
7
PostgreSQL full text search types are mapped onto .NET types built-in to Npgsql. The `tsvector` type is mapped to `NpgsqlTsVector` and `tsquery` is mapped to `NpgsqlTsQuery`. This means you can use properties of type `NpgsqlTsVector` directly in your model to create `tsvector` columns. The `NpgsqlTsQuery` type on the other hand, is used in LINQ queries.
8
8
9
-
```c#
9
+
```csharp
10
10
publicclassProduct
11
11
{
12
12
publicintId { get; set; }
@@ -24,7 +24,7 @@ public class Product
24
24
25
25
This method adds a `tsvector` column to your table, that is automatically updated when the row is modified. First, add an `NpgsqlTsVector` property to your entity:
26
26
27
-
```c#
27
+
```csharp
28
28
publicclassProduct
29
29
{
30
30
publicintId { get; set; }
@@ -43,7 +43,7 @@ Setting up the column to be auto-updated depends on your PostgreSQL version. On
43
43
44
44
The following will set up a generated `tsvector` column, over which you can easily create an index:
@@ -99,7 +99,7 @@ public partial class CreateProductTable : Migration
99
99
100
100
Once your auto-updated `tsvector` column is set up, any inserts or updates on the `Products` table will now update the `SearchVector` column and maintain it automatically. You can query it as follows:
101
101
102
-
```c#
102
+
```csharp
103
103
varcontext=newProductDbContext();
104
104
varnpgsql=context.Products
105
105
.Where(p=>p.SearchVector.Matches("Npgsql"))
@@ -112,7 +112,7 @@ Version 5.0.0 of the provider includes sugar for defining the appropriate expres
112
112
113
113
#### [Version 5.0.0](#tab/v5)
114
114
115
-
```c#
115
+
```csharp
116
116
modelBuilder.Entity<Blog>()
117
117
.HasIndex(b=>new { b.Title, b.Description })
118
118
.HasMethod("GIN")
@@ -123,7 +123,7 @@ modelBuilder.Entity<Blog>()
123
123
124
124
Create a migration which will contain the index creation SQL (`dotnet ef migrations add ...`). At this point, open the generated migration with your editor and add the following:
Copy file name to clipboardExpand all lines: conceptual/EFCore.PG/mapping/general.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ The EF Core provider transparently maps the types supported by Npgsql at the ADO
4
4
5
5
This means that you can use PostgreSQL-specific types, such as `inet` or `circle`, directly in your entities. Simply define your properties just as if they were a simple type, such as a `string`:
6
6
7
-
```c#
7
+
```csharp
8
8
publicclassMyEntity
9
9
{
10
10
publicintId { get; set; }
@@ -25,14 +25,14 @@ In some cases, your .NET property type can be mapped to several PostgreSQL data
@@ -67,7 +67,7 @@ Npgsql's support for `ToJson()` is fully aligned with the general EF support; se
67
67
68
68
To get you started quickly, assume that we have the following Customer type, with a Details property that we want to map to a single JSON column in the database:
69
69
70
-
```c#
70
+
```csharp
71
71
publicclassCustomer
72
72
{
73
73
publicintId { get; set; }
@@ -90,7 +90,7 @@ public class Order // Part of the JSON column
90
90
91
91
To instruct EF to map CustomerDetails - and within it, Order - to a JSON column, configure it as follows:
@@ -178,7 +178,7 @@ Note that when using this mapping, only limited forms of LINQ querying is suppor
178
178
179
179
If your column JSON schema isn't stable, a strongly-typed POCO mapping may not be appropriate. The Npgsql provider also allows you to map the DOM document type provided by [System.Text.Json APIs](https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-apis/).
180
180
181
-
```c#
181
+
```csharp
182
182
publicclassSomeEntity : IDisposable
183
183
{
184
184
publicintId { get; set; }
@@ -192,7 +192,7 @@ Note that neither a data annotation nor the fluent API are required, as [JsonDoc
192
192
193
193
Once a document is loaded from the database, you can traverse it:
@@ -30,7 +30,7 @@ This configures all aspects of Npgsql to use the NodaTime plugin - both at the E
30
30
31
31
If you're creating an external NpgsqlDataSource and passing it to `UseNpgsql`, you must call `UseNodaTime` on your NpgsqlDataSourceBuilder independently of the EF-level setup:
### [Older EF versions, with a connection string](#tab/legacy-with-connection-string)
44
44
45
-
```c#
45
+
```csharp
46
46
// Configure UseNodaTime at the ADO.NET level.
47
47
// This code must be placed at the beginning of your application, before any other Npgsql API is called; an appropriate place for this is in the static constructor on your DbContext class:
The above sets up all the necessary mappings and operation translators. You can now use NodaTime types as regular properties in your entities, and even perform some operations:
The provider knows how to translate many members and methods on mapped NodaTime types. For example, the following query will be translated to SQL and evaluated server-side:
0 commit comments