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
Copy file name to clipboardexpand all lines: efcore/mapping/enum.html
+55-33
Original file line number
Diff line number
Diff line change
@@ -87,67 +87,89 @@ <h1 id="enum-type-mapping">Enum Type Mapping</h1>
87
87
88
88
<p>By default, any enum properties in your model will be mapped to database integers. EF Core 2.1 also allows you to map these to strings in the database with value converters.</p>
89
89
<p>However, the Npgsql provider also allows you to map your CLR enums to <ahref="https://www.postgresql.org/docs/current/static/datatype-enum.html">database enum types</a>. This option, unique to PostgreSQL, provides the best of both worlds: the enum is internally stored in the database as a number (minimal storage), but is handled like a string (more usable, no need to remember numeric values) and has type safety.</p>
90
-
<h2id="creating-your-database-enum">Creating your database enum</h2>
90
+
<h2id="setting-up-your-enum-with-ef">Setting up your enum with EF</h2>
91
+
<divclass="NOTE">
92
+
<h5>Note</h5>
93
+
<p>Enum mapping has changed considerably in EF 9.0.</p>
94
+
</div>
95
+
<p>If you're using EF 9.0 or above, simply call <code>MapEnum</code> inside your <code>UseNpgsql</code> invocation.</p>
96
+
<divclass="tabGroup" id="tabgroup_bHGHmlrG6S">
97
+
<ulrole="tablist">
98
+
<lirole="presentation">
99
+
<ahref="#tabpanel_bHGHmlrG6S_with-connection-string" role="tab" aria-controls="tabpanel_bHGHmlrG6S_with-connection-string" data-tab="with-connection-string" tabindex="0" aria-selected="true">With a connection string</a>
100
+
</li>
101
+
<lirole="presentation">
102
+
<ahref="#tabpanel_bHGHmlrG6S_with-datasource" role="tab" aria-controls="tabpanel_bHGHmlrG6S_with-datasource" data-tab="with-datasource" tabindex="-1">With an external NpgsqlDataSource</a>
o => o.MapEnum<Mood>("mood")));
111
+
</code></pre>
112
+
<p>This configures all aspects of Npgsql to use your <code>Mood</code> enum - both at the EF and the lower-level Npgsql layer - and ensures that the enum is created in the database in EF migrations.</p>
<p>If you're creating an external NpgsqlDataSource and passing it to <code>UseNpgsql</code>, you must make sure to map your enum on that data independently of the EF-level setup:</p>
117
+
<pre><codeclass="lang-c#">var dataSourceBuilder = new NpgsqlDataSourceBuilder("<connection string>");
o => o.MapEnum<Mood>("mood")));
124
+
</code></pre>
125
+
</section>
126
+
</div>
127
+
<h3id="older-ef-versions">Older EF versions</h3>
128
+
<p>On versions of EF prior to 9.0, enum setup is more involved and consists of several steps; enum mapping has to be done at the lower-level Npgsql layer, and also requires explicit configuration in the EF model for creation in the database via migrations.</p>
129
+
<h4id="creating-your-database-enum">Creating your database enum</h4>
91
130
<p>First, you must specify the PostgreSQL enum type on your model, just like you would with tables, sequences or other databases objects:</p>
<p>This causes the EF Core provider to create your enum type, <code>mood</code>, with two labels: <code>happy</code> and <code>sad</code>. This will cause the appropriate migration to be created.</p>
96
-
<p>If you are using <code>context.Database.Migrate()</code> to create your enums, you need to instruct Npgsql to reload all types after applying your migrations:</p>
<p>Since version 7.0, NpgsqlDataSource is the recommended way to use Npgsql. When using NpgsqlDataSource, map your enum when building your data source:</p>
126
-
<pre><codeclass="lang-c#">// Call UseNodaTime() when building your data source:
149
+
<pre><codeclass="lang-c#">// Call MapEnum() when building your data source:
127
150
var dataSourceBuilder = new NpgsqlDataSourceBuilder(/* connection string */);
<p>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, <em>before</em> any EF Core operations take place. An appropriate place for this is in the static constructor on your DbContext class:</p>
<p>This code lets Npgsql know that your CLR enum type, <code>Mood</code>, should be mapped to a database enum called <code>mood</code>. Note that if your enum is in a custom schema (not <code>public</code>), you must specify that schema in the call to <code>MapEnum</code>.</p>
144
-
<p>If you're curious as to inner workings, this code maps the enum with the ADO.NET provider - <ahref="http://www.npgsql.org/doc/types/enums_and_composites.html">see here for the full docs</a>. When the Npgsql EF Core first initializes, it calls into the ADO.NET provider to get all mapped enums, and sets everything up internally at the EF Core layer as well.</p>
145
163
<divclass="NOTE">
146
164
<h5>Note</h5>
147
165
<p>If you have multiple context types, all <code>MapEnum</code> invocations must be done before <em>any</em> of them is used; this means that the code cannot be in your static constructors, but must be moved to the program start.</p>
148
166
</div>
167
+
</section>
168
+
</div>
169
+
170
+
<p>This code lets Npgsql know that your CLR enum type, <code>Mood</code>, should be mapped to a database enum called <code>mood</code>. Note that if your enum is in a custom schema (not <code>public</code>), you must specify that schema in the call to <code>MapEnum</code>.</p>
<p>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 <ahref="https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/managing?tabs=dotnet-core-cli#arbitrary-changes-via-raw-sql">raw SQL</a> in your migrations as follows:</p>
169
-
<pre><codeclass="lang-c#">migrationBuilder.Sql(@"ALTER TYPE mood RENAME VALUE 'happy' TO 'thrilled';");
191
+
<pre><codeclass="lang-c#">migrationBuilder.Sql("ALTER TYPE mood RENAME VALUE 'happy' TO 'thrilled';");
170
192
</code></pre>
171
193
<p>As always, test your migrations carefully before running them on production databases.</p>
172
194
<h2id="scaffolding-from-an-existing-database">Scaffolding from an existing database</h2>
173
-
<p>If you're creating your model from an existing database, the provider will recognize enums in your database, and scaffold the appropriate <code>HasPostgresEnum()</code> lines in your model. However, the scaffolding process has no knowledge of your CLR type, and will therefore skip your enum columns (warnings will be logged). You will have to create the CLR type, add the global mapping and add the properties to your entities.</p>
174
-
<p>In the future it may be possible to scaffold the actual enum type (and with it the properties), but this doesn't happen at the moment.</p>
195
+
<p>If you're creating your model from an existing database, the provider will recognize enums in your database, and scaffold the appropriate <code>HasPostgresEnum()</code> lines in your model. However, the scaffolding process has no knowledge of your CLR type, and will therefore skip your enum columns (warnings will be logged). You will have to create the CLR typeand perform the proper setup as described above.</p>
196
+
<p>In the future it may be possible to scaffold the actual enum type (and with it the properties), but this isn't supported at the moment.</p>
Copy file name to clipboardexpand all lines: efcore/mapping/nodatime.html
+24-14
Original file line number
Diff line number
Diff line change
@@ -94,40 +94,50 @@ <h2 id="what-is-nodatime">What is NodaTime</h2>
94
94
<li>NodaTime types can fully represent PostgreSQL's microsecond precision, and can represent dates outside the BCL's date limit (1AD-9999AD).</li>
95
95
</ul>
96
96
<h2id="setup">Setup</h2>
97
-
<p>To set up the NodaTime plugin, add the <ahref="https://www.nuget.org/packages/Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime">Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime nuget</a> to your project. Then, configure NodaTime as follows:</p>
97
+
<p>To set up the NodaTime plugin, add the <ahref="https://www.nuget.org/packages/Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime">Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime nuget</a> to your project. Then, configure the NodaTime plugin as follows:</p>
<ahref="#tabpanel_bHGHmlrG6S_with-datasource" role="tab" aria-controls="tabpanel_bHGHmlrG6S_with-datasource" data-tab="with-datasource" tabindex="-1">With an external NpgsqlDataSource</a>
105
+
</li>
106
+
<lirole="presentation">
107
+
<ahref="#tabpanel_bHGHmlrG6S_legacy-with-connection-string" role="tab" aria-controls="tabpanel_bHGHmlrG6S_legacy-with-connection-string" data-tab="legacy-with-connection-string" tabindex="-1">Older EF versions, with a connection string</a>
<p>Since version 7.0, NpgsqlDataSource is the recommended way to use Npgsql. When using NpsgqlDataSource, NodaTime currently has to be configured twice - once at the EF level, and once at the underlying ADO.NET level (there are plans to improve this):</p>
110
-
<pre><codeclass="lang-c#">// Call UseNodaTime() when building your data source:
111
-
var dataSourceBuilder = new NpgsqlDataSourceBuilder(/* connection string */);
121
+
<p>If you're creating an external NpgsqlDataSource and passing it to <code>UseNpgsql</code>, you must call <code>UseNodaTime</code> on your NpgsqlDataSourceBuilder independently of the EF-level setup:</p>
122
+
<pre><codeclass="lang-c#">var dataSourceBuilder = new NpgsqlDataSourceBuilder("<connection string>");
112
123
dataSourceBuilder.UseNodaTime();
113
124
var dataSource = dataSourceBuilder.Build();
114
125
115
-
// Then, when configuring EF Core with UseNpgsql(), call UseNodaTime() there as well:
<p>Since version 7.0, NpgsqlDataSource is the recommended way to use Npgsql. However, if you're not yet using NpgsqlDataSource, configure NodaTime as follows:</p>
123
-
<pre><codeclass="lang-c#">// Configure NodaTime at the ADO.NET level.
133
+
<pre><codeclass="lang-c#">// Configure UseNodaTime at the ADO.NET level.
124
134
// 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:
0 commit comments