Skip to content
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
10 changes: 10 additions & 0 deletions src/StronglyTypedIds/Templates/Guid/Guid_EfCoreValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,14 @@ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversio
value => new TESTID(value),
mappingHints
) { }
}

public class EfCoreValueGenerator : Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator<TESTID>
{
public override bool GeneratesTemporaryValues => false;

public override TESTID Next(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry)
{
return TESTID.New();
}
}
12 changes: 12 additions & 0 deletions src/StronglyTypedIds/Templates/Int/Int_EfCoreValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,16 @@ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversio
value => new TESTID(value),
mappingHints
) { }
}

public class EfCoreValueGenerator : Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator<TESTID>
{
private int _id = int.MinValue;
public override bool GeneratesTemporaryValues => true;

public override TESTID Next(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry)
{
System.Threading.Interlocked.Increment(ref _id);
return new TESTID(_id);
}
}
12 changes: 12 additions & 0 deletions src/StronglyTypedIds/Templates/Long/Long_EfCoreValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,16 @@ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversio
value => new TESTID(value),
mappingHints
) { }
}

public class EfCoreValueGenerator : Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator<TESTID>
{
private long _id = long.MinValue;
public override bool GeneratesTemporaryValues => true;

public override TESTID Next(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry)
{
System.Threading.Interlocked.Increment(ref _id);
return new TESTID(_id);
}
}
41 changes: 41 additions & 0 deletions test/StronglyTypedIds.IntegrationTests/LongIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,32 @@ public void WhenEfCoreValueConverterUsesValueConverter()
Assert.Equal(original.Id, retrieved.Id);
}
}

[Fact]
public void WhenEfCoreValueGenerationUsesValueGenerator()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();

var options = new DbContextOptionsBuilder<TestDbContext>()
.UseSqlite(connection)
.Options;

var original = new GenerationTestEntity();
using (var context = new TestDbContext(options))
{
context.Database.EnsureCreated();
context.GenerationEntities.Add(original);
context.SaveChanges();
}

using (var context = new TestDbContext(options))
{
var all = context.GenerationEntities.ToList();
var retrieved = Assert.Single(all);
Assert.Equal(1, retrieved.Id.Value);
}
}

[Fact]
public async Task WhenDapperValueConverterUsesValueConverter()
Expand Down Expand Up @@ -322,6 +348,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
public class TestDbContext : DbContext
{
public DbSet<TestEntity> Entities { get; set; }
public DbSet<GenerationTestEntity> GenerationEntities { get; set; }

public TestDbContext(DbContextOptions options) : base(options)
{
Expand All @@ -337,6 +364,15 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.HasConversion(new EfCoreLongId.EfCoreValueConverter())
.ValueGeneratedNever();
});
modelBuilder
.Entity<GenerationTestEntity>(builder =>
{
builder
.Property(x => x.Id)
.HasConversion(new EfCoreLongId.EfCoreValueConverter())
.HasValueGenerator<EfCoreLongId.EfCoreValueGenerator>()
.ValueGeneratedOnAdd();
});
}
}

Expand All @@ -345,6 +381,11 @@ public class TestEntity
public EfCoreLongId Id { get; set; }
}

public class GenerationTestEntity
{
public EfCoreLongId Id { get; set; }
}

public class EntityWithNullableId
{
public NewtonsoftJsonLongId? Id { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,15 @@ namespace Some.Namespace
mappingHints
) { }
}

public class EfCoreValueGenerator : Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator<MyTestId>
{
public override bool GeneratesTemporaryValues => false;

public override MyTestId Next(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry)
{
return MyTestId.New();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,15 @@ namespace Some.Namespace
mappingHints
) { }
}

public class EfCoreValueGenerator : Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator<MyTestId>
{
public override bool GeneratesTemporaryValues => false;

public override MyTestId Next(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry)
{
return MyTestId.New();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,15 @@ namespace Some.Namespace
mappingHints
) { }
}

public class EfCoreValueGenerator : Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator<MyTestId>
{
public override bool GeneratesTemporaryValues => false;

public override MyTestId Next(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry)
{
return MyTestId.New();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,15 @@ namespace Some.Namespace
mappingHints
) { }
}

public class EfCoreValueGenerator : Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator<MyTestId>
{
public override bool GeneratesTemporaryValues => false;

public override MyTestId Next(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry)
{
return MyTestId.New();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,17 @@ namespace Some.Namespace
mappingHints
) { }
}

public class EfCoreValueGenerator : Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator<MyTestId>
{
private int _id = int.MinValue;
public override bool GeneratesTemporaryValues => true;

public override MyTestId Next(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry)
{
System.Threading.Interlocked.Increment(ref _id);
return new MyTestId(_id);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,17 @@ namespace Some.Namespace
mappingHints
) { }
}

public class EfCoreValueGenerator : Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator<MyTestId>
{
private int _id = int.MinValue;
public override bool GeneratesTemporaryValues => true;

public override MyTestId Next(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry)
{
System.Threading.Interlocked.Increment(ref _id);
return new MyTestId(_id);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,17 @@ namespace Some.Namespace
mappingHints
) { }
}

public class EfCoreValueGenerator : Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator<MyTestId>
{
private int _id = int.MinValue;
public override bool GeneratesTemporaryValues => true;

public override MyTestId Next(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry)
{
System.Threading.Interlocked.Increment(ref _id);
return new MyTestId(_id);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,17 @@ namespace Some.Namespace
mappingHints
) { }
}

public class EfCoreValueGenerator : Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator<MyTestId>
{
private int _id = int.MinValue;
public override bool GeneratesTemporaryValues => true;

public override MyTestId Next(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry)
{
System.Threading.Interlocked.Increment(ref _id);
return new MyTestId(_id);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,17 @@ namespace Some.Namespace
mappingHints
) { }
}

public class EfCoreValueGenerator : Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator<MyTestId>
{
private long _id = long.MinValue;
public override bool GeneratesTemporaryValues => true;

public override MyTestId Next(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry)
{
System.Threading.Interlocked.Increment(ref _id);
return new MyTestId(_id);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,17 @@ namespace Some.Namespace
mappingHints
) { }
}

public class EfCoreValueGenerator : Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator<MyTestId>
{
private long _id = long.MinValue;
public override bool GeneratesTemporaryValues => true;

public override MyTestId Next(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry)
{
System.Threading.Interlocked.Increment(ref _id);
return new MyTestId(_id);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,17 @@ namespace Some.Namespace
mappingHints
) { }
}

public class EfCoreValueGenerator : Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator<MyTestId>
{
private long _id = long.MinValue;
public override bool GeneratesTemporaryValues => true;

public override MyTestId Next(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry)
{
System.Threading.Interlocked.Increment(ref _id);
return new MyTestId(_id);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,17 @@ namespace Some.Namespace
mappingHints
) { }
}

public class EfCoreValueGenerator : Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator<MyTestId>
{
private long _id = long.MinValue;
public override bool GeneratesTemporaryValues => true;

public override MyTestId Next(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry)
{
System.Threading.Interlocked.Increment(ref _id);
return new MyTestId(_id);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ namespace Some.Namespace
mappingHints
) { }
}

public class EfCoreValueGenerator : Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator<MyTestId>
{
public override bool GeneratesTemporaryValues => false;

public override MyTestId Next(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry)
{
return MyTestId.New();
}
}

class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ namespace Some.Namespace
mappingHints
) { }
}

public class EfCoreValueGenerator : Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator<MyTestId>
{
public override bool GeneratesTemporaryValues => false;

public override MyTestId Next(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry)
{
return MyTestId.New();
}
}

class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ namespace Some.Namespace
mappingHints
) { }
}

public class EfCoreValueGenerator : Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator<MyTestId>
{
public override bool GeneratesTemporaryValues => false;

public override MyTestId Next(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry)
{
return MyTestId.New();
}
}

class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ namespace Some.Namespace
mappingHints
) { }
}

public class EfCoreValueGenerator : Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator<MyTestId>
{
public override bool GeneratesTemporaryValues => false;

public override MyTestId Next(Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entry)
{
return MyTestId.New();
}
}

class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
{
Expand Down
Loading