Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SqlServer support #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>Adds views, synonyms, stored procedures, etc. (so-called SQL objects) to the EF model. Creates migrations, when those objects are changed.
SQL objects are defined as raw SQL in C#-code or in embedded resources. They can be even generated at runtime.
For more information refer to https://github.com/CUSTIS-public/CUSTIS.NetCore.EF.MigrationGenerationExtensions</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.10" />
</ItemGroup>


<ItemGroup>
<ProjectReference Include="..\CUSTIS.NetCore.EF.MigrationGenerationExtensions\CUSTIS.NetCore.EF.MigrationGenerationExtensions.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using CUSTIS.NetCore.EF.MigrationGenerationExtensions.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Migrations;

namespace CUSTIS.NetCore.EF.MigrationGenerationExtensions.PostgreSQL
{
/// <summary> Extensions for using SQL objects </summary>
public static class ConfigurationExtensions
{
/// <summary> Replaces some services necessary for SQL objects </summary>
public static void UseSqlObjects(this DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseCommonSqlObjects();

optionsBuilder.ReplaceService<IMigrationsSqlGenerator, CustomSqlServerMigrationsSqlGenerator>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using CUSTIS.NetCore.EF.MigrationGenerationExtensions.Generation;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.SqlServer.Design.Internal;

namespace CUSTIS.NetCore.EF.MigrationGenerationExtensions.PostgreSQL
{
#pragma warning disable EF1001 // Internal EF Core API usage.
internal class CustomSqlServerAnnotationCodeGenerator : SqlServerAnnotationCodeGenerator
{
public CustomSqlServerAnnotationCodeGenerator(AnnotationCodeGeneratorDependencies dependencies) : base(dependencies)
{
}
#pragma warning restore EF1001 // Internal EF Core API usage.

/// <inheritdoc />
public override IEnumerable<IAnnotation> FilterIgnoredAnnotations(IEnumerable<IAnnotation> annotations)
{
return base.FilterIgnoredAnnotations(annotations).FilterCustomIgnoredAnnotations();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using CUSTIS.NetCore.EF.MigrationGenerationExtensions.Configuration;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.DependencyInjection;

namespace CUSTIS.NetCore.EF.MigrationGenerationExtensions.PostgreSQL
{
/// <summary> Services necessary to create migrations </summary>
public class CustomSqlServerDesignTimeServices : CustomDesignTimeServices
{
/// <inheritdoc />
public override void ConfigureDesignTimeServices(IServiceCollection services)
{
ReplaceBySingleton<IAnnotationCodeGenerator, CustomSqlServerAnnotationCodeGenerator>(services);
base.ConfigureDesignTimeServices(services);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using CUSTIS.NetCore.EF.MigrationGenerationExtensions.Generation;
using CUSTIS.NetCore.EF.MigrationGenerationExtensions.Generation.Contracts;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations.Operations;

namespace CUSTIS.NetCore.EF.MigrationGenerationExtensions.PostgreSQL
{
/// <summary> Генератор SQL кода </summary>
public sealed class CustomSqlServerMigrationsSqlGenerator : SqlServerMigrationsSqlGenerator
{
private readonly IReadOnlyDictionary<Type, ICustomSqlGenerator> _generators;

#pragma warning disable EF1001 // Internal EF Core API usage.
/// <summary> Генератор SQL кода </summary>
public CustomSqlServerMigrationsSqlGenerator(MigrationsSqlGeneratorDependencies dependencies,
IRelationalAnnotationProvider migrationsAnnotations,
IEnumerable<ICustomSqlGenerator> sqlGenerators) : base(dependencies, migrationsAnnotations)
#pragma warning restore EF1001 // Internal EF Core API usage.
{
_generators = sqlGenerators.ToDictionary(g => g.OperationType);
}

/// <inheritdoc />
protected override void Generate(
MigrationOperation operation,
IModel? model,
MigrationCommandListBuilder builder)
{
if (operation is CustomMigrationOperation customOp && _generators.ContainsKey(operation.GetType()))
{
_generators[operation.GetType()].Generate(customOp, builder);
return;
}

base.Generate(operation, model, builder);
}
}
}
14 changes: 10 additions & 4 deletions src/CUSTIS.NetCore.EF.MigrationGenerationExtensions.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31829.152
# Visual Studio Version 17
VisualStudioVersion = 17.4.33110.190
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Common", "Common", "{1C048004-EFD9-44F7-BF26-4F6596DEB5B5}"
ProjectSection(SolutionItems) = preProject
Expand All @@ -14,14 +14,16 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Common", "Common", "{1C0480
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CUSTIS.NetCore.EF.MigrationGenerationExtensions.PostgreSQL", "CUSTIS.NetCore.EF.MigrationGenerationExtensions.PostgreSQL\CUSTIS.NetCore.EF.MigrationGenerationExtensions.PostgreSQL.csproj", "{907E5D6B-DB59-4285-97E3-499F3CB0722F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestEntryPoint", "TestEntryPoint\TestEntryPoint.csproj", "{10A0A624-ED5A-414C-BCB7-97AB31D41C53}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestEntryPoint", "TestEntryPoint\TestEntryPoint.csproj", "{10A0A624-ED5A-414C-BCB7-97AB31D41C53}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestDataAccessLayer", "TestDataAccessLayer\TestDataAccessLayer.csproj", "{D408BA8B-B0F4-4E4E-BF85-5895FACCE332}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestDataAccessLayer", "TestDataAccessLayer\TestDataAccessLayer.csproj", "{D408BA8B-B0F4-4E4E-BF85-5895FACCE332}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTests", "UnitTests\UnitTests.csproj", "{C8004252-7BF1-4FDC-9E87-043D118EA93B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CUSTIS.NetCore.EF.MigrationGenerationExtensions", "CUSTIS.NetCore.EF.MigrationGenerationExtensions\CUSTIS.NetCore.EF.MigrationGenerationExtensions.csproj", "{ED274B3C-04B6-4794-87F9-D8769A93E49E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CUSTIS.NetCore.EF.MigrationGenerationExtensions.SqlServer", "CUSTIS.NetCore.EF.MigrationGenerationExtensions.SqlServer\CUSTIS.NetCore.EF.MigrationGenerationExtensions.SqlServer.csproj", "{A1E65443-5350-4A9F-A026-85EA732A6035}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -48,6 +50,10 @@ Global
{ED274B3C-04B6-4794-87F9-D8769A93E49E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ED274B3C-04B6-4794-87F9-D8769A93E49E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ED274B3C-04B6-4794-87F9-D8769A93E49E}.Release|Any CPU.Build.0 = Release|Any CPU
{A1E65443-5350-4A9F-A026-85EA732A6035}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A1E65443-5350-4A9F-A026-85EA732A6035}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A1E65443-5350-4A9F-A026-85EA732A6035}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A1E65443-5350-4A9F-A026-85EA732A6035}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down