Skip to content

Commit

Permalink
add EF core 7.0 support (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdihaghshenas authored and bricelam committed Sep 29, 2022
1 parent 683a4c0 commit 4fc0b5d
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 92 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Copyright>© 2022 Brice Lambson, et al. All rights reserved.</Copyright>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)key.snk</AssemblyOriginatorKeyFile>
<Version>3.0.1</Version>
<Version>4.0.0-rc.1</Version>
<Features>strict</Features>
<LangVersion>latest</LangVersion>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.0-rc.1.22426.7" />
</ItemGroup>

<ItemGroup>
Expand Down
41 changes: 17 additions & 24 deletions EFCore.SqlServer.HierarchyId.Test/CSharpDbContextGeneratorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,32 @@ public void Generates_context_with_UseHierarchyId()
@"using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
namespace TestNamespace
namespace TestNamespace;
public partial class TestDbContext : DbContext
{
public partial class TestDbContext : DbContext
public TestDbContext()
{
public TestDbContext()
{
}
}
public TestDbContext(DbContextOptions<TestDbContext> options)
: base(options)
{
}
public TestDbContext(DbContextOptions<TestDbContext> options)
: base(options)
{
}
public virtual DbSet<Patriarch> Patriarch { get; set; }
public virtual DbSet<Patriarch> Patriarch { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
optionsBuilder.UseSqlServer(""Initial Catalog=TestDatabase"", x => x.UseHierarchyId());
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
OnModelCreatingPartial(modelBuilder);
}
=> optionsBuilder.UseSqlServer(""Initial Catalog=TestDatabase"", x => x.UseHierarchyId());
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
",
code.ContextFile);
Expand Down
51 changes: 25 additions & 26 deletions EFCore.SqlServer.HierarchyId.Test/CSharpEntityTypeGeneratorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ public void Class_with_HierarchyId_key_is_generated()
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace TestNamespace
namespace TestNamespace;
public partial class Patriarch
{
public partial class Patriarch
{
[Key]
public HierarchyId Id { get; set; }
public string Name { get; set; }
}
[Key]
public HierarchyId Id { get; set; }
public string Name { get; set; }
}
",
code.AdditionalFiles.Single(f => f.Path == "Patriarch.cs"));
Expand Down Expand Up @@ -68,17 +67,17 @@ public void Class_with_HierarchyId_property_is_generated()
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace TestNamespace
namespace TestNamespace;
public partial class Patriarch
{
public partial class Patriarch
{
[Key]
public int Id { get; set; }
public HierarchyId Hierarchy { get; set; }
public string Name { get; set; }
}
[Key]
public int Id { get; set; }
public HierarchyId Hierarchy { get; set; }
public string Name { get; set; }
}
",
code.AdditionalFiles.Single(f => f.Path == "Patriarch.cs"));
Expand Down Expand Up @@ -108,17 +107,17 @@ public void Class_with_multiple_HierarchyId_properties_are_generated()
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace TestNamespace
namespace TestNamespace;
public partial class Patriarch
{
public partial class Patriarch
{
[Key]
public HierarchyId Id { get; set; }
public HierarchyId Hierarchy { get; set; }
public string Name { get; set; }
}
[Key]
public HierarchyId Id { get; set; }
public HierarchyId Hierarchy { get; set; }
public string Name { get; set; }
}
",
code.AdditionalFiles.Single(f => f.Path == "Patriarch.cs"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<IsPackable>false</IsPackable>
<AssemblyName>EntityFrameworkCore.SqlServer.HierarchyId.Test</AssemblyName>
<RootNamespace>Microsoft.EntityFrameworkCore.SqlServer</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0-1.final" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational.Specification.Tests" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="6.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0-2.final" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational.Specification.Tests" Version="7.0.0-rc.1.22426.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0-rc.1.22426.7" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="7.0.0-rc.1.22426.10" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,22 @@ public override string GetExpectedMigrationCode(string migrationName, string roo
#nullable disable
namespace {rootNamespace}.Migrations
#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
namespace MyApp.Data.Migrations
{{
public partial class {migrationName} : Migration
/// <inheritdoc />
public partial class MyMigration : Migration
{{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{{
migrationBuilder.CreateTable(
name: ""{nameof(ConvertedTestModels)}"",
name: ""ConvertedTestModels"",
columns: table => new
{{
Id = table.Column<int>(type: ""int"", nullable: false),
HierarchyId = table.Column<{nameof(HierarchyId)}>(type: ""hierarchyid"", nullable: true),
HierarchyId = table.Column<HierarchyId>(type: ""hierarchyid"", nullable: true),
Name = table.Column<string>(type: ""nvarchar(max)"", nullable: true)
}},
constraints: table =>
Expand All @@ -52,15 +56,15 @@ protected override void Up(MigrationBuilder migrationBuilder)
}});
migrationBuilder.CreateTable(
name: ""{nameof(TestModels)}"",
name: ""TestModels"",
columns: table => new
{{
{nameof(Patriarch.Id)} = table.Column<{nameof(HierarchyId)}>(type: ""hierarchyid"", nullable: false),
{nameof(Patriarch.Name)} = table.Column<string>(type: ""nvarchar(max)"", nullable: true)
Id = table.Column<HierarchyId>(type: ""hierarchyid"", nullable: false),
Name = table.Column<string>(type: ""nvarchar(max)"", nullable: true)
}},
constraints: table =>
{{
table.PrimaryKey(""PK_{nameof(TestModels)}"", x => x.{nameof(Patriarch.Id)});
table.PrimaryKey(""PK_TestModels"", x => x.Id);
}});
migrationBuilder.InsertData(
Expand All @@ -84,13 +88,14 @@ protected override void Up(MigrationBuilder migrationBuilder)
}});
}}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{{
migrationBuilder.DropTable(
name: ""ConvertedTestModels"");
migrationBuilder.DropTable(
name: ""{nameof(TestModels)}"");
name: ""TestModels"");
}}
}}
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,65 +32,70 @@ public override string GetExpectedMigrationCode(string migrationName, string roo
#nullable disable
namespace {rootNamespace}.Migrations
#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
namespace MyApp.Data.Migrations
{{
public partial class {migrationName} : Migration
/// <inheritdoc />
public partial class MyMigration : Migration
{{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{{
migrationBuilder.CreateTable(
name: ""{nameof(ConvertedTestModels)}"",
name: ""ConvertedTestModels"",
columns: table => new
{{
{nameof(ConvertedPatriarch.Id)} = table.Column<int>(type: ""int"", nullable: false),
{nameof(ConvertedPatriarch.HierarchyId)} = table.Column<{nameof(HierarchyId)}>(type: ""hierarchyid"", nullable: true),
{nameof(ConvertedPatriarch.Name)} = table.Column<string>(type: ""nvarchar(max)"", nullable: true)
Id = table.Column<int>(type: ""int"", nullable: false),
HierarchyId = table.Column<HierarchyId>(type: ""hierarchyid"", nullable: true),
Name = table.Column<string>(type: ""nvarchar(max)"", nullable: true)
}},
constraints: table =>
{{
table.PrimaryKey(""PK_{nameof(ConvertedTestModels)}"", x => x.{nameof(ConvertedPatriarch.Id)});
table.PrimaryKey(""PK_ConvertedTestModels"", x => x.Id);
}});
migrationBuilder.CreateTable(
name: ""{nameof(TestModels)}"",
name: ""TestModels"",
columns: table => new
{{
{nameof(Patriarch.Id)} = table.Column<{nameof(HierarchyId)}>(type: ""hierarchyid"", nullable: false),
{nameof(Patriarch.Name)} = table.Column<string>(type: ""nvarchar(max)"", nullable: true)
Id = table.Column<HierarchyId>(type: ""hierarchyid"", nullable: false),
Name = table.Column<string>(type: ""nvarchar(max)"", nullable: true)
}},
constraints: table =>
{{
table.PrimaryKey(""PK_{nameof(TestModels)}"", x => x.{nameof(Patriarch.Id)});
table.PrimaryKey(""PK_TestModels"", x => x.Id);
}});
migrationBuilder.InsertData(
table: ""ConvertedTestModels"",
columns: new[] {{ ""Id"", ""HierarchyId"", ""Name"" }},
values: new object[,]
{{
{{ 1, {typeof(HierarchyId).FullName}.Parse(""/""), ""Eddard Stark"" }},
{{ 2, {typeof(HierarchyId).FullName}.Parse(""/1/""), ""Robb Stark"" }},
{{ 3, {typeof(HierarchyId).FullName}.Parse(""/2/""), ""Jon Snow"" }}
{{ 1, Microsoft.EntityFrameworkCore.HierarchyId.Parse(""/""), ""Eddard Stark"" }},
{{ 2, Microsoft.EntityFrameworkCore.HierarchyId.Parse(""/1/""), ""Robb Stark"" }},
{{ 3, Microsoft.EntityFrameworkCore.HierarchyId.Parse(""/2/""), ""Jon Snow"" }}
}});
migrationBuilder.InsertData(
table: ""TestModels"",
columns: new[] {{ ""Id"", ""Name"" }},
values: new object[,]
{{
{{ {typeof(HierarchyId).FullName}.Parse(""/""), ""Eddard Stark"" }},
{{ {typeof(HierarchyId).FullName}.Parse(""/1/""), ""Robb Stark"" }},
{{ {typeof(HierarchyId).FullName}.Parse(""/2/""), ""Jon Snow"" }}
{{ Microsoft.EntityFrameworkCore.HierarchyId.Parse(""/""), ""Eddard Stark"" }},
{{ Microsoft.EntityFrameworkCore.HierarchyId.Parse(""/1/""), ""Robb Stark"" }},
{{ Microsoft.EntityFrameworkCore.HierarchyId.Parse(""/2/""), ""Jon Snow"" }}
}});
}}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{{
migrationBuilder.DropTable(
name: ""ConvertedTestModels"");
migrationBuilder.DropTable(
name: ""{nameof(TestModels)}"");
name: ""TestModels"");
}}
}}
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public FakeScaffoldingModelFactory(
LoggingDefinitions loggingDefinitions,
IModelRuntimeInitializer modelRuntimeInitializer)
: base(reporter, candidateNamingService, pluralizer, cSharpUtilities, scaffoldingTypeMapper,
loggingDefinitions, modelRuntimeInitializer)
modelRuntimeInitializer)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ private SqlServerTestHelpers()
public override IServiceCollection AddProviderServices(IServiceCollection services)
=> services.AddEntityFrameworkSqlServer();

public override void UseProviderOptions(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(new SqlConnection("Database=DummyDatabase"));
public override DbContextOptionsBuilder UseProviderOptions(DbContextOptionsBuilder optionsBuilder)
{
return optionsBuilder.UseSqlServer(new SqlConnection("Database=DummyDatabase"));
}
//public override void UseProviderOptions(DbContextOptionsBuilder optionsBuilder)
// => optionsBuilder.UseSqlServer(new SqlConnection("Database=DummyDatabase"));

public override LoggingDefinitions LoggingDefinitions { get; } = new SqlServerLoggingDefinitions();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0-rc.1.22426.7" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ protected override string GenerateNonNullSqlLiteral(object value)
//command, and the value is already a hierarchyid
return $"'{value}'";
}

public override DbParameter CreateParameter(DbCommand command, string name, object value, bool? nullable = null)
public override DbParameter CreateParameter(DbCommand command, string name, object value, bool? nullable = null, ParameterDirection direction = ParameterDirection.Input)
{
var parameter = command.CreateParameter();
parameter.Direction = ParameterDirection.Input;
Expand Down

0 comments on commit 4fc0b5d

Please sign in to comment.