Skip to content

Commit 9debbc9

Browse files
committed
Add NpgsqlCSharpRuntimeAnnotationCodeGenerator
Closes #2308
1 parent 7be1f48 commit 9debbc9

File tree

2 files changed

+230
-1
lines changed

2 files changed

+230
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.EntityFrameworkCore.Design.Internal;
5+
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Internal;
6+
7+
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Design.Internal;
8+
9+
/// <summary>
10+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
11+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
12+
/// any release. You should only use it directly in your code with extreme caution and knowing that
13+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
14+
/// </summary>
15+
#pragma warning disable EF1001 // Internal EF Core API usage.
16+
public class NpgsqlCSharpRuntimeAnnotationCodeGenerator : RelationalCSharpRuntimeAnnotationCodeGenerator
17+
{
18+
/// <summary>
19+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
20+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
21+
/// any release. You should only use it directly in your code with extreme caution and knowing that
22+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
23+
/// </summary>
24+
public NpgsqlCSharpRuntimeAnnotationCodeGenerator(
25+
CSharpRuntimeAnnotationCodeGeneratorDependencies dependencies,
26+
RelationalCSharpRuntimeAnnotationCodeGeneratorDependencies relationalDependencies)
27+
: base(dependencies, relationalDependencies)
28+
{
29+
}
30+
31+
/// <inheritdoc />
32+
public override void Generate(IModel model, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
33+
{
34+
if (!parameters.IsRuntime)
35+
{
36+
var annotations = parameters.Annotations;
37+
annotations.Remove(NpgsqlAnnotationNames.DatabaseTemplate);
38+
annotations.Remove(NpgsqlAnnotationNames.Tablespace);
39+
annotations.Remove(NpgsqlAnnotationNames.CollationDefinitionPrefix);
40+
41+
#pragma warning disable CS0618
42+
annotations.Remove(NpgsqlAnnotationNames.DefaultColumnCollation);
43+
#pragma warning restore CS0618
44+
45+
foreach (var annotationName in annotations.Keys.Where(
46+
k =>
47+
k.StartsWith(NpgsqlAnnotationNames.PostgresExtensionPrefix, StringComparison.Ordinal)
48+
|| k.StartsWith(NpgsqlAnnotationNames.EnumPrefix, StringComparison.Ordinal)
49+
|| k.StartsWith(NpgsqlAnnotationNames.RangePrefix, StringComparison.Ordinal)))
50+
{
51+
annotations.Remove(annotationName);
52+
}
53+
}
54+
55+
base.Generate(model, parameters);
56+
}
57+
58+
/// <inheritdoc />
59+
public override void Generate(IRelationalModel model, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
60+
{
61+
if (!parameters.IsRuntime)
62+
{
63+
var annotations = parameters.Annotations;
64+
annotations.Remove(NpgsqlAnnotationNames.DatabaseTemplate);
65+
annotations.Remove(NpgsqlAnnotationNames.Tablespace);
66+
annotations.Remove(NpgsqlAnnotationNames.CollationDefinitionPrefix);
67+
68+
#pragma warning disable CS0618
69+
annotations.Remove(NpgsqlAnnotationNames.DefaultColumnCollation);
70+
#pragma warning restore CS0618
71+
72+
foreach (var annotationName in annotations.Keys.Where(
73+
k =>
74+
k.StartsWith(NpgsqlAnnotationNames.PostgresExtensionPrefix, StringComparison.Ordinal)
75+
|| k.StartsWith(NpgsqlAnnotationNames.EnumPrefix, StringComparison.Ordinal)
76+
|| k.StartsWith(NpgsqlAnnotationNames.RangePrefix, StringComparison.Ordinal)))
77+
{
78+
annotations.Remove(annotationName);
79+
}
80+
}
81+
82+
base.Generate(model, parameters);
83+
}
84+
85+
/// <inheritdoc />
86+
public override void Generate(IProperty property, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
87+
{
88+
if (!parameters.IsRuntime)
89+
{
90+
var annotations = parameters.Annotations;
91+
annotations.Remove(NpgsqlAnnotationNames.IdentityOptions);
92+
annotations.Remove(NpgsqlAnnotationNames.TsVectorConfig);
93+
annotations.Remove(NpgsqlAnnotationNames.TsVectorProperties);
94+
annotations.Remove(NpgsqlAnnotationNames.CompressionMethod);
95+
96+
if (!annotations.ContainsKey(NpgsqlAnnotationNames.ValueGenerationStrategy))
97+
{
98+
annotations[NpgsqlAnnotationNames.ValueGenerationStrategy] = property.GetValueGenerationStrategy();
99+
}
100+
}
101+
102+
base.Generate(property, parameters);
103+
}
104+
105+
/// <inheritdoc />
106+
public override void Generate(IColumn column, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
107+
{
108+
if (!parameters.IsRuntime)
109+
{
110+
var annotations = parameters.Annotations;
111+
112+
annotations.Remove(NpgsqlAnnotationNames.IdentityOptions);
113+
annotations.Remove(NpgsqlAnnotationNames.TsVectorConfig);
114+
annotations.Remove(NpgsqlAnnotationNames.TsVectorProperties);
115+
annotations.Remove(NpgsqlAnnotationNames.CompressionMethod);
116+
}
117+
118+
base.Generate(column, parameters);
119+
}
120+
121+
/// <inheritdoc />
122+
public override void Generate(IIndex index, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
123+
{
124+
if (!parameters.IsRuntime)
125+
{
126+
var annotations = parameters.Annotations;
127+
128+
annotations.Remove(RelationalAnnotationNames.Collation);
129+
annotations.Remove(NpgsqlAnnotationNames.IndexMethod);
130+
annotations.Remove(NpgsqlAnnotationNames.IndexOperators);
131+
annotations.Remove(NpgsqlAnnotationNames.IndexSortOrder);
132+
annotations.Remove(NpgsqlAnnotationNames.IndexNullSortOrder);
133+
annotations.Remove(NpgsqlAnnotationNames.IndexInclude);
134+
annotations.Remove(NpgsqlAnnotationNames.CreatedConcurrently);
135+
annotations.Remove(NpgsqlAnnotationNames.NullsDistinct);
136+
137+
foreach (var annotationName in annotations.Keys.Where(
138+
k => k.StartsWith(NpgsqlAnnotationNames.StorageParameterPrefix, StringComparison.Ordinal)))
139+
{
140+
annotations.Remove(annotationName);
141+
}
142+
}
143+
144+
base.Generate(index, parameters);
145+
}
146+
147+
/// <inheritdoc />
148+
public override void Generate(ITableIndex index, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
149+
{
150+
if (!parameters.IsRuntime)
151+
{
152+
var annotations = parameters.Annotations;
153+
154+
annotations.Remove(RelationalAnnotationNames.Collation);
155+
annotations.Remove(NpgsqlAnnotationNames.IndexMethod);
156+
annotations.Remove(NpgsqlAnnotationNames.IndexOperators);
157+
annotations.Remove(NpgsqlAnnotationNames.IndexSortOrder);
158+
annotations.Remove(NpgsqlAnnotationNames.IndexNullSortOrder);
159+
annotations.Remove(NpgsqlAnnotationNames.IndexInclude);
160+
annotations.Remove(NpgsqlAnnotationNames.CreatedConcurrently);
161+
annotations.Remove(NpgsqlAnnotationNames.NullsDistinct);
162+
163+
foreach (var annotationName in annotations.Keys.Where(
164+
k => k.StartsWith(NpgsqlAnnotationNames.StorageParameterPrefix, StringComparison.Ordinal)))
165+
{
166+
annotations.Remove(annotationName);
167+
}
168+
}
169+
170+
base.Generate(index, parameters);
171+
}
172+
173+
/// <inheritdoc />
174+
public override void Generate(IEntityType entityType, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
175+
{
176+
if (!parameters.IsRuntime)
177+
{
178+
var annotations = parameters.Annotations;
179+
180+
annotations.Remove(NpgsqlAnnotationNames.UnloggedTable);
181+
annotations.Remove(CockroachDbAnnotationNames.InterleaveInParent);
182+
183+
foreach (var annotationName in annotations.Keys.Where(
184+
k => k.StartsWith(NpgsqlAnnotationNames.StorageParameterPrefix, StringComparison.Ordinal)))
185+
{
186+
annotations.Remove(annotationName);
187+
}
188+
}
189+
190+
base.Generate(entityType, parameters);
191+
}
192+
193+
/// <inheritdoc />
194+
public override void Generate(ITable table, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
195+
{
196+
if (!parameters.IsRuntime)
197+
{
198+
var annotations = parameters.Annotations;
199+
200+
annotations.Remove(NpgsqlAnnotationNames.UnloggedTable);
201+
annotations.Remove(CockroachDbAnnotationNames.InterleaveInParent);
202+
203+
foreach (var annotationName in annotations.Keys.Where(
204+
k => k.StartsWith(NpgsqlAnnotationNames.StorageParameterPrefix, StringComparison.Ordinal)))
205+
{
206+
annotations.Remove(annotationName);
207+
}
208+
}
209+
210+
base.Generate(table, parameters);
211+
}
212+
213+
/// <inheritdoc />
214+
public override void Generate(IRelationalPropertyOverrides overrides, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
215+
{
216+
if (!parameters.IsRuntime)
217+
{
218+
var annotations = parameters.Annotations;
219+
annotations.Remove(NpgsqlAnnotationNames.IdentityIncrement);
220+
annotations.Remove(NpgsqlAnnotationNames.IdentitySeed);
221+
}
222+
223+
base.Generate(overrides, parameters);
224+
}
225+
}

src/EFCore.PG/Design/Internal/NpgsqlDesignTimeServices.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Npgsql.EntityFrameworkCore.PostgreSQL.Scaffolding.Internal;
1+
using Microsoft.EntityFrameworkCore.Design.Internal;
2+
using Npgsql.EntityFrameworkCore.PostgreSQL.Scaffolding.Internal;
23

34
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Design.Internal;
45

@@ -21,7 +22,10 @@ public virtual void ConfigureDesignTimeServices(IServiceCollection serviceCollec
2122
Check.NotNull(serviceCollection, nameof(serviceCollection));
2223

2324
serviceCollection.AddEntityFrameworkNpgsql();
25+
#pragma warning disable EF1001 // Internal EF Core API usage.
2426
new EntityFrameworkRelationalDesignServicesBuilder(serviceCollection)
27+
.TryAdd<ICSharpRuntimeAnnotationCodeGenerator, NpgsqlCSharpRuntimeAnnotationCodeGenerator>()
28+
#pragma warning restore EF1001 // Internal EF Core API usage.
2529
.TryAdd<IAnnotationCodeGenerator, NpgsqlAnnotationCodeGenerator>()
2630
.TryAdd<IDatabaseModelFactory, NpgsqlDatabaseModelFactory>()
2731
.TryAdd<IProviderConfigurationCodeGenerator, NpgsqlCodeGenerator>()

0 commit comments

Comments
 (0)