Skip to content

Commit 590f4d3

Browse files
committed
Add support for EF Core global conversions
Fixes #50
1 parent 5242f13 commit 590f4d3

File tree

782 files changed

+1903
-748
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

782 files changed

+1903
-748
lines changed

src/StronglyTypedIds/Templates/Guid/Guid_EfCoreValueConverter.cs

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

22
public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter<TESTID, System.Guid>
33
{
4+
public EfCoreValueConverter() : this(null) { }
45
public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
56
: base(
67
id => id.Value,

src/StronglyTypedIds/Templates/Int/Int_EfCoreValueConverter.cs

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

22
public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter<TESTID, int>
33
{
4+
public EfCoreValueConverter() : this(null) { }
45
public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
56
: base(
67
id => id.Value,

src/StronglyTypedIds/Templates/Long/Long_EfCoreValueConverter.cs

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

22
public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter<TESTID, long>
33
{
4+
public EfCoreValueConverter() : this(null) { }
45
public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
56
: base(
67
id => id.Value,

src/StronglyTypedIds/Templates/NewId/NewId_EfCoreValueConverter.cs

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

22
public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter<TESTID, System.Guid>
33
{
4+
public EfCoreValueConverter() : this(null) { }
45
public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
56
: base(
67
id => id.Value.ToGuid(),

src/StronglyTypedIds/Templates/NullableString/NullableString_EfCoreValueConverter.cs

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

22
public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter<TESTID, string>
33
{
4+
public EfCoreValueConverter() : this(null) { }
45
public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null)
56
: base(
67
id => id.Value!,

src/StronglyTypedIds/Templates/String/String_EfCoreValueConverter.cs

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

22
public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter<TESTID, string>
33
{
4+
public EfCoreValueConverter() : this(null) { }
45
public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
56
: base(
67
id => id.Value,

test/StronglyTypedIds.IntegrationTests/DefaultIdTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,59 @@ public void TypeConverter_CanConvertToAndFrom(string value)
122122
Assert.Equal(value, reconverted);
123123
}
124124

125+
#if NET6_0_OR_GREATER
126+
[Fact]
127+
public void WhenConventionBasedEfCoreValueConverterUsesValueConverter()
128+
{
129+
var connection = new SqliteConnection("DataSource=:memory:");
130+
connection.Open();
131+
132+
var options = new DbContextOptionsBuilder<ConventionsDbContext>()
133+
.UseSqlite(connection)
134+
.Options;
135+
136+
using (var context = new ConventionsDbContext(options))
137+
{
138+
context.Database.EnsureCreated();
139+
context.Entities.Add(
140+
new TestEntity { Id = EfCoreDefaultId.New() });
141+
context.SaveChanges();
142+
}
143+
using (var context = new ConventionsDbContext(options))
144+
{
145+
var all = context.Entities.ToList();
146+
Assert.Single(all);
147+
}
148+
}
149+
150+
public class ConventionsDbContext : DbContext
151+
{
152+
public DbSet<TestEntity> Entities { get; set; }
153+
154+
public ConventionsDbContext(DbContextOptions options) : base(options)
155+
{
156+
}
157+
158+
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
159+
{
160+
configurationBuilder
161+
.Properties<EfCoreDefaultId>()
162+
.HaveConversion<EfCoreDefaultId.EfCoreValueConverter>();
163+
}
164+
165+
protected override void OnModelCreating(ModelBuilder modelBuilder)
166+
{
167+
modelBuilder
168+
.Entity<TestEntity>(builder =>
169+
{
170+
builder
171+
.Property(x => x.Id)
172+
.ValueGeneratedNever();
173+
});
174+
}
175+
}
176+
#endif
177+
125178
public class TestDbContext : DbContext
126179
{
127180
public DbSet<TestEntity> Entities { get; set; }

test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,59 @@ public void ImplementsInterfaces()
272272
#pragma warning restore 184
273273
}
274274

275+
#if NET6_0_OR_GREATER
276+
[Fact]
277+
public void WhenConventionBasedEfCoreValueConverterUsesValueConverter()
278+
{
279+
var connection = new SqliteConnection("DataSource=:memory:");
280+
connection.Open();
281+
282+
var options = new DbContextOptionsBuilder<ConventionsDbContext>()
283+
.UseSqlite(connection)
284+
.Options;
285+
286+
using (var context = new ConventionsDbContext(options))
287+
{
288+
context.Database.EnsureCreated();
289+
context.Entities.Add(
290+
new TestEntity { Id = EfCoreGuidId.New() });
291+
context.SaveChanges();
292+
}
293+
using (var context = new ConventionsDbContext(options))
294+
{
295+
var all = context.Entities.ToList();
296+
Assert.Single(all);
297+
}
298+
}
299+
300+
public class ConventionsDbContext : DbContext
301+
{
302+
public DbSet<TestEntity> Entities { get; set; }
303+
304+
public ConventionsDbContext(DbContextOptions options) : base(options)
305+
{
306+
}
307+
308+
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
309+
{
310+
configurationBuilder
311+
.Properties<EfCoreGuidId>()
312+
.HaveConversion<EfCoreGuidId.EfCoreValueConverter>();
313+
}
314+
315+
protected override void OnModelCreating(ModelBuilder modelBuilder)
316+
{
317+
modelBuilder
318+
.Entity<TestEntity>(builder =>
319+
{
320+
builder
321+
.Property(x => x.Id)
322+
.ValueGeneratedNever();
323+
});
324+
}
325+
}
326+
#endif
327+
275328
public class TestDbContext : DbContext
276329
{
277330
public DbSet<TestEntity> Entities { get; set; }

test/StronglyTypedIds.IntegrationTests/IntIdTests.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,60 @@ public void ImplementsInterfaces()
264264
#pragma warning restore 184
265265
}
266266

267+
268+
#if NET6_0_OR_GREATER
269+
[Fact]
270+
public void WhenConventionBasedEfCoreValueConverterUsesValueConverter()
271+
{
272+
var connection = new SqliteConnection("DataSource=:memory:");
273+
connection.Open();
274+
275+
var options = new DbContextOptionsBuilder<ConventionsDbContext>()
276+
.UseSqlite(connection)
277+
.Options;
278+
279+
using (var context = new ConventionsDbContext(options))
280+
{
281+
context.Database.EnsureCreated();
282+
context.Entities.Add(
283+
new TestEntity { Id = new EfCoreIntId(123) });
284+
context.SaveChanges();
285+
}
286+
using (var context = new ConventionsDbContext(options))
287+
{
288+
var all = context.Entities.ToList();
289+
Assert.Single(all);
290+
}
291+
}
292+
293+
public class ConventionsDbContext : DbContext
294+
{
295+
public DbSet<TestEntity> Entities { get; set; }
296+
297+
public ConventionsDbContext(DbContextOptions options) : base(options)
298+
{
299+
}
300+
301+
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
302+
{
303+
configurationBuilder
304+
.Properties<EfCoreIntId>()
305+
.HaveConversion<EfCoreIntId.EfCoreValueConverter>();
306+
}
307+
308+
protected override void OnModelCreating(ModelBuilder modelBuilder)
309+
{
310+
modelBuilder
311+
.Entity<TestEntity>(builder =>
312+
{
313+
builder
314+
.Property(x => x.Id)
315+
.ValueGeneratedNever();
316+
});
317+
}
318+
}
319+
#endif
320+
267321
public class TestDbContext : DbContext
268322
{
269323
public DbSet<TestEntity> Entities { get; set; }

test/StronglyTypedIds.IntegrationTests/LongIdTests.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,60 @@ public void ImplementsInterfaces()
265265
#pragma warning restore 184
266266
}
267267

268+
269+
#if NET6_0_OR_GREATER
270+
[Fact]
271+
public void WhenConventionBasedEfCoreValueConverterUsesValueConverter()
272+
{
273+
var connection = new SqliteConnection("DataSource=:memory:");
274+
connection.Open();
275+
276+
var options = new DbContextOptionsBuilder<ConventionsDbContext>()
277+
.UseSqlite(connection)
278+
.Options;
279+
280+
using (var context = new ConventionsDbContext(options))
281+
{
282+
context.Database.EnsureCreated();
283+
context.Entities.Add(
284+
new TestEntity { Id = new EfCoreLongId(123) });
285+
context.SaveChanges();
286+
}
287+
using (var context = new ConventionsDbContext(options))
288+
{
289+
var all = context.Entities.ToList();
290+
Assert.Single(all);
291+
}
292+
}
293+
294+
public class ConventionsDbContext : DbContext
295+
{
296+
public DbSet<TestEntity> Entities { get; set; }
297+
298+
public ConventionsDbContext(DbContextOptions options) : base(options)
299+
{
300+
}
301+
302+
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
303+
{
304+
configurationBuilder
305+
.Properties<EfCoreLongId>()
306+
.HaveConversion<EfCoreLongId.EfCoreValueConverter>();
307+
}
308+
309+
protected override void OnModelCreating(ModelBuilder modelBuilder)
310+
{
311+
modelBuilder
312+
.Entity<TestEntity>(builder =>
313+
{
314+
builder
315+
.Property(x => x.Id)
316+
.ValueGeneratedNever();
317+
});
318+
}
319+
}
320+
#endif
321+
268322
public class TestDbContext : DbContext
269323
{
270324
public DbSet<TestEntity> Entities { get; set; }

0 commit comments

Comments
 (0)