Skip to content

Commit ddd7d27

Browse files
authored
Merge pull request #20 from TAGC/multiple-providers-bug
Support instantiating DbContexts with multiple providers in same execution context
2 parents dfb4310 + 26255d9 commit ddd7d27

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

src/SpatialFocus.EntityFrameworkCore.Extensions/EnumLookupExtension.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ namespace SpatialFocus.EntityFrameworkCore.Extensions
1717

1818
public static class EnumLookupExtension
1919
{
20-
private static List<Type> ConcreteTypeSeededList { get; set; } = new List<Type>();
21-
2220
public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLookupOptions enumOptions)
2321
{
2422
foreach (IMutableProperty property in modelBuilder.Model.GetEntityTypes().SelectMany(x => x.GetProperties()).ToList())
@@ -88,6 +86,7 @@ private static void ConfigureEnumLookupForProperty(ModelBuilder modelBuilder, En
8886
bool usesDescription = enumValueDescriptions.Values.Any(x => x != null);
8987

9088
Type enumLookupEntityType = GetEnumLookupEntityType(enumOptions, usesDescription, enumType);
89+
bool shouldSkipEnumLookupTableConfiguration = modelBuilder.Model.FindEntityType(enumLookupEntityType) != null;
9190

9291
configureEntityType(enumLookupEntityType);
9392

@@ -98,13 +97,11 @@ private static void ConfigureEnumLookupForProperty(ModelBuilder modelBuilder, En
9897
configureEntityTypeConversion(valueConverter);
9998
}
10099

101-
if (ConcreteTypeSeededList.Contains(enumLookupEntityType))
100+
if (shouldSkipEnumLookupTableConfiguration)
102101
{
103102
return;
104103
}
105104

106-
ConcreteTypeSeededList.Add(enumLookupEntityType);
107-
108105
EntityTypeBuilder enumLookupBuilder = modelBuilder.Entity(enumLookupEntityType);
109106
ConfigureEnumLookupTable(enumLookupBuilder, enumOptions, enumType);
110107

src/SpatialFocus.EntityFrameworkCore.Extensions/SpatialFocus.EntityFrameworkCore.Extensions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66

77
<PropertyGroup>
8-
<Version>2.1.2</Version>
8+
<Version>2.1.3</Version>
99
<PackageId>SpatialFocus.EntityFrameworkCore.Extensions</PackageId>
1010
<Title>Spatial Focus EntityFrameworkCore Extensions</Title>
1111
<Description>A set of useful extensions for EntityFrameworkCore (Enum Lookup Tables, Naming of tables / properties / keys, Pluralize).</Description>

test/SpatialFocus.EntityFrameworkCore.Extensions.Test/NamingOptionsTest.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace SpatialFocus.EntityFrameworkCore.Extensions.Test
88
using System;
99
using System.Linq;
1010
using Microsoft.EntityFrameworkCore;
11+
using Microsoft.EntityFrameworkCore.ChangeTracking;
1112
using Microsoft.EntityFrameworkCore.Metadata;
1213
using Microsoft.EntityFrameworkCore.Storage;
1314
using SpatialFocus.EntityFrameworkCore.Extensions.Test.Entities;
@@ -88,5 +89,32 @@ public void SetNamingScheme()
8889
Assert.True(findEntityType.GetForeignKeys().All(x => x.GetConstraintName() == NamingScheme.SnakeCase(x.GetDefaultName())));
8990
Assert.True(findEntityType.GetIndexes().All(x => x.GetName() == NamingScheme.SnakeCase(x.GetDefaultName())));
9091
}
92+
93+
[Fact]
94+
public void MultipleProviders()
95+
{
96+
DbContextOptions<ChangeTrackerInConstructorContext> inMemoryOptions = new DbContextOptionsBuilder<ChangeTrackerInConstructorContext>()
97+
.UseInMemoryDatabase(Guid.NewGuid().ToString(), new InMemoryDatabaseRoot())
98+
.Options;
99+
100+
ChangeTrackerInConstructorContext _ = new ChangeTrackerInConstructorContext(inMemoryOptions, EnumLookupOptions.Default, null);
101+
102+
DbContextOptions<ChangeTrackerInConstructorContext> sqliteOptions = new DbContextOptionsBuilder<ChangeTrackerInConstructorContext>()
103+
.UseSqlite("Filename=:memory:")
104+
.Options;
105+
106+
ChangeTrackerInConstructorContext context = new ChangeTrackerInConstructorContext(sqliteOptions, EnumLookupOptions.Default, null);
107+
context.Database.EnsureCreated();
108+
context.Dispose();
109+
}
110+
111+
private class ChangeTrackerInConstructorContext : ProductContext
112+
{
113+
public ChangeTrackerInConstructorContext(DbContextOptions options, EnumLookupOptions enumLookupOptions, NamingOptions namingOptions)
114+
: base(options, enumLookupOptions, namingOptions)
115+
{
116+
ChangeTracker _ = ChangeTracker;
117+
}
118+
}
91119
}
92120
}

test/SpatialFocus.EntityFrameworkCore.Extensions.Test/SpatialFocus.EntityFrameworkCore.Extensions.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<ItemGroup>
1010
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.0" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.0" />
1112
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
1213
<PackageReference Include="xunit" Version="2.4.0" />
1314
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />

0 commit comments

Comments
 (0)