Skip to content
Merged
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
Expand Up @@ -64,7 +64,8 @@ internal static class PropertyAccessor
// instance => converter(body)
var invokeConverter = Expression.Invoke(converter, converterInput);

if (body.Type.IsClass)
// If the property is a reference type, we need to check for null before calling the converter
if (body.Type.IsClass && !invokeConverter.Type.IsValueType)
{
// instance => body == null ? null : converter(body)
var nullCondition = Expression.Equal(body, Expression.Constant(null, body.Type));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

using SmartEnum.EFCore;

namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;

public class TestDbContext : TestDbContextBase
Expand All @@ -11,13 +13,16 @@ public class TestDbContext : TestDbContextBase
public DbSet<TestEntityWithGuidId> TestEntitiesWithGuidId { get; set; } = null!;
public DbSet<TestEntityWithConverters> TestEntitiesWithConverter { get; set; } = null!;
public DbSet<TestEntityWithComplexType> TestEntitiesWithComplexType { get; set; } = null!;
public DbSet<TestEntityWithSmartEnum> TestEntitiesWithSmartEnum { get; set; } = null!;
public DbSet<Student> Students { get; set; } = null!;
public DbSet<Course> Courses { get; set; } = null!;

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.ConfigureSmartEnum();

modelBuilder.Entity<TestEntityWithConverters>(builder =>
{
builder.Property(e => e.CreatedAt)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.EntityFrameworkCore;

namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;

[PrimaryKey(nameof(Id))]
public class TestEntityWithSmartEnum : TestEntityBase
{
public int Id { get; set; }

public TestSmartEnum Enum { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Ardalis.SmartEnum;

namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;

public class TestSmartEnum : SmartEnum<TestSmartEnum>
{
private TestSmartEnum(string name, int value) : base(name, value)
{
}

public static readonly TestSmartEnum Value = new TestSmartEnum("test", 1);

Check warning on line 11 in tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/DbContext/TestSmartEnum.cs

View workflow job for this annotation

GitHub Actions / build (net8.0, 8.0.x)

'TestSmartEnum.Value' hides inherited member 'SmartEnum<TestSmartEnum, int>.Value'. Use the new keyword if hiding was intended.

Check warning on line 11 in tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/DbContext/TestSmartEnum.cs

View workflow job for this annotation

GitHub Actions / build (net8.0, 8.0.x)

'TestSmartEnum.Value' hides inherited member 'SmartEnum<TestSmartEnum, int>.Value'. Use the new keyword if hiding was intended.

Check warning on line 11 in tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/DbContext/TestSmartEnum.cs

View workflow job for this annotation

GitHub Actions / build (net9.0, 9.0.x)

'TestSmartEnum.Value' hides inherited member 'SmartEnum<TestSmartEnum, int>.Value'. Use the new keyword if hiding was intended.

Check warning on line 11 in tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/DbContext/TestSmartEnum.cs

View workflow job for this annotation

GitHub Actions / build (net9.0, 9.0.x)

'TestSmartEnum.Value' hides inherited member 'SmartEnum<TestSmartEnum, int>.Value'. Use the new keyword if hiding was intended.
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql.NetTopologySuite" Version="9.0.0" />
</ItemGroup>

<ItemGroup Label="Specific dependencies">
<PackageReference Include="Ardalis.SmartEnum.EFCore" Version="8.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\PhenX.EntityFrameworkCore.BulkInsert.MySql\PhenX.EntityFrameworkCore.BulkInsert.MySql.csproj" />
<ProjectReference Include="..\..\src\PhenX.EntityFrameworkCore.BulkInsert.Oracle\PhenX.EntityFrameworkCore.BulkInsert.Oracle.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using FluentAssertions;

using PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContainer;
using PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;

using Xunit;

namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.Tests.Various;

public abstract class VariousTestsBase<TDbContext>(TestDbContainer dbContainer) : IAsyncLifetime
where TDbContext : TestDbContext, new()
{
private readonly Guid _run = Guid.NewGuid();
private TDbContext _context = null!;

public async Task InitializeAsync()
{
_context = await dbContainer.CreateContextAsync<TDbContext>("various");
}

public Task DisposeAsync()
{
_context.Dispose();
return Task.CompletedTask;
}

[SkippableTheory]
[CombinatorialData]
public async Task InsertSmartEnumEntities(InsertStrategy strategy)
{
// Arrange
var entities = new List<TestEntityWithSmartEnum>
{
new TestEntityWithSmartEnum { TestRun = _run, Enum = TestSmartEnum.Value},
new TestEntityWithSmartEnum { TestRun = _run, Enum = TestSmartEnum.Value}
};

// Act
var insertedEntities = await _context.InsertWithStrategyAsync(strategy, entities);

// Assert
insertedEntities.Should().BeEquivalentTo(entities,
o => o.RespectingRuntimeTypes().Excluding(e => e.Id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContainer;
using PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;

using Xunit;

namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.Tests.Various;

[Trait("Category", "MySql")]
[Collection(TestDbContainerMySqlCollection.Name)]
public class VariousTestsMySql(TestDbContainerMySql dbContainer) : VariousTestsBase<TestDbContextMySql>(dbContainer)
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContainer;
using PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;

using Xunit;

namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.Tests.Various;

[Trait("Category", "Oracle")]
[Collection(TestDbContainerOracleCollection.Name)]
public class VariousTestsOracle(TestDbContainerOracle dbContainer) : VariousTestsBase<TestDbContextOracle>(dbContainer)
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContainer;
using PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;

using Xunit;

namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.Tests.Various;

[Trait("Category", "PostgreSql")]
[Collection(TestDbContainerPostgreSqlCollection.Name)]
public class VariousTestsPostgreSql(TestDbContainerPostgreSql dbContainer) : VariousTestsBase<TestDbContextPostgreSql>(dbContainer)
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContainer;
using PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;

using Xunit;

namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.Tests.Various;

[Trait("Category", "SqlServer")]
[Collection(TestDbContainerSqlServerCollection.Name)]
public class VariousTestsSqlServer(TestDbContainerSqlServer dbContainer) : VariousTestsBase<TestDbContextSqlServer>(dbContainer)
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContainer;
using PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext;

using Xunit;

namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.Tests.Various;

[Trait("Category", "Sqlite")]
[Collection(TestDbContainerSqliteCollection.Name)]
public class VariousTestsSqlite(TestDbContainerSqlite dbContainer) : VariousTestsBase<TestDbContextSqlite>(dbContainer)
{
}