-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
SqlServerConventionSetBuilder.cs
129 lines (105 loc) · 6.81 KB
/
SqlServerConventionSetBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Metadata.Conventions.Internal;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.EntityFrameworkCore.Metadata.Conventions
{
/// <summary>
/// <para>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </para>
/// <para>
/// The service lifetime is <see cref="ServiceLifetime.Scoped"/> and multiple registrations
/// are allowed. This means that each <see cref="DbContext"/> instance will use its own
/// set of instances of this service.
/// The implementations may depend on other services registered with any lifetime.
/// The implementations do not need to be thread-safe.
/// </para>
/// </summary>
[EntityFrameworkInternal]
public class SqlServerConventionSetBuilder : RelationalConventionSetBuilder
{
private readonly ISqlGenerationHelper _sqlGenerationHelper;
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
[EntityFrameworkInternal]
public SqlServerConventionSetBuilder(
[NotNull] ProviderConventionSetBuilderDependencies dependencies,
[NotNull] RelationalConventionSetBuilderDependencies relationalDependencies,
[NotNull] ISqlGenerationHelper sqlGenerationHelper)
: base(dependencies, relationalDependencies)
{
_sqlGenerationHelper = sqlGenerationHelper;
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
[EntityFrameworkInternal]
public override ConventionSet CreateConventionSet()
{
var conventionSet = base.CreateConventionSet();
var logger = Dependencies.Logger;
var valueGenerationStrategyConvention = new SqlServerValueGenerationStrategyConvention(logger);
conventionSet.ModelInitializedConventions.Add(valueGenerationStrategyConvention);
conventionSet.ModelInitializedConventions.Add(new RelationalMaxIdentifierLengthConvention(128));
ValueGeneratorConvention valueGeneratorConvention = new SqlServerValueGeneratorConvention(logger);
ReplaceConvention(conventionSet.BaseEntityTypeChangedConventions, valueGeneratorConvention);
var sqlServerInMemoryTablesConvention = new SqlServerMemoryOptimizedTablesConvention(logger);
conventionSet.EntityTypeAnnotationChangedConventions.Add(sqlServerInMemoryTablesConvention);
ReplaceConvention(conventionSet.PrimaryKeyChangedConventions, valueGeneratorConvention);
conventionSet.KeyAddedConventions.Add(sqlServerInMemoryTablesConvention);
ReplaceConvention(conventionSet.ForeignKeyAddedConventions, valueGeneratorConvention);
ReplaceConvention(conventionSet.ForeignKeyRemovedConventions, valueGeneratorConvention);
var sqlServerIndexConvention = new SqlServerIndexConvention(_sqlGenerationHelper,logger);
conventionSet.BaseEntityTypeChangedConventions.Add(sqlServerIndexConvention);
conventionSet.ModelBuiltConventions.Add(valueGenerationStrategyConvention);
conventionSet.IndexAddedConventions.Add(sqlServerInMemoryTablesConvention);
conventionSet.IndexAddedConventions.Add(sqlServerIndexConvention);
conventionSet.IndexUniquenessChangedConventions.Add(sqlServerIndexConvention);
conventionSet.IndexAnnotationChangedConventions.Add(sqlServerIndexConvention);
conventionSet.PropertyNullabilityChangedConventions.Add(sqlServerIndexConvention);
conventionSet.PropertyAnnotationChangedConventions.Add(sqlServerIndexConvention);
conventionSet.PropertyAnnotationChangedConventions.Add((SqlServerValueGeneratorConvention)valueGeneratorConvention);
ReplaceConvention(conventionSet.ModelAnnotationChangedConventions, (RelationalDbFunctionConvention)new SqlServerDbFunctionConvention(logger));
return conventionSet;
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
[EntityFrameworkInternal]
public static ConventionSet Build()
{
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkSqlServer()
.AddDbContext<DbContext>((p, o) =>
o.UseSqlServer("Server=.")
.UseInternalServiceProvider(p))
.BuildServiceProvider();
using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
using (var context = serviceScope.ServiceProvider.GetService<DbContext>())
{
return ConventionSet.CreateConventionSet(context);
}
}
}
}
}