Skip to content

Commit

Permalink
refactor: move lastchanged EF config in separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneD committed Feb 13, 2024
1 parent 4630e1f commit 97f4c96
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
]
},
"dotnet-ef": {
"version": "5.0.1",
"version": "6.0.3",
"commands": [
"dotnet-ef"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Be.Vlaanderen.Basisregisters.ProjectionHandling.LastChangedList
using Microsoft.EntityFrameworkCore.Design;
using Model;
using Runner;
using Runner.ProjectionStates;

public class LastChangedListContext : RunnerDbContext<LastChangedListContext>
{
Expand All @@ -20,53 +21,10 @@ public LastChangedListContext(DbContextOptions<LastChangedListContext> options)

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

modelBuilder
.Entity<LastChangedRecord>()
.Property(x => x.ToBeIndexed)
.ValueGeneratedOnAddOrUpdate()
.HasComputedColumnSql("CAST(CASE WHEN (([Position] > [LastPopulatedPosition]) AND ([ErrorCount] < 10)) THEN 1 ELSE 0 END AS bit) PERSISTED");

modelBuilder
.Entity<LastChangedRecord>()
.HasIndex(x => x.ToBeIndexed)
.IncludeProperties(x => x.LastError);

modelBuilder
.Entity<LastChangedRecord>()
.HasIndex(x => x.Position);

modelBuilder
.Entity<LastChangedRecord>()
.HasIndex(x => x.CacheKey);

modelBuilder
.Entity<LastChangedRecord>()
.HasIndex(x => x.ErrorCount);

modelBuilder
.Entity<LastChangedRecord>()
.HasIndex(x => x.LastError);

modelBuilder
.Entity<LastChangedRecord>()
.HasIndex(x => new { x.ToBeIndexed, x.LastError });

modelBuilder
.Entity<LastChangedRecord>()
.HasKey(x => x.Id)
.IsClustered();

modelBuilder
.Entity<LastChangedRecord>()
.HasIndex(x => new
{
x.Position,
x.LastPopulatedPosition,
x.ErrorCount,
x.LastError
});
modelBuilder.ApplyConfiguration(new ProjectionStatesConfiguration(ProjectionStateSchema));
modelBuilder.ApplyConfiguration(new LastChangedRecordConfiguration(TableName, Schema));

modelBuilder
.HasDefaultSchema(Schema);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Be.Vlaanderen.Basisregisters.ProjectionHandling.LastChangedList.Model
{
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

public class LastChangedRecord
{
Expand All @@ -18,4 +20,48 @@ public class LastChangedRecord

public bool ToBeIndexed { get; private set; }
}

public class LastChangedRecordConfiguration : IEntityTypeConfiguration<LastChangedRecord>
{
private readonly string _tableName;
private readonly string _schema;

public LastChangedRecordConfiguration(string tableName, string schema)
{
_tableName = tableName;
_schema = schema;
}

public LastChangedRecordConfiguration()
{ }

public void Configure(EntityTypeBuilder<LastChangedRecord> builder)
{
builder
.ToTable(_tableName, _schema)
.HasKey(x => x.Id)
.IsClustered();

builder
.Property(x => x.ToBeIndexed)
.ValueGeneratedOnAddOrUpdate()
.HasComputedColumnSql("CAST(CASE WHEN (([Position] > [LastPopulatedPosition]) AND ([ErrorCount] < 10)) THEN 1 ELSE 0 END AS bit) PERSISTED");

builder
.HasIndex(x => x.ToBeIndexed)
.IncludeProperties(x => x.LastError);
builder.HasIndex(x => x.Position);
builder.HasIndex(x => x.CacheKey);
builder.HasIndex(x => x.ErrorCount);
builder.HasIndex(x => x.LastError);
builder.HasIndex(x => new { x.ToBeIndexed, x.LastError });
builder.HasIndex(x => new
{
x.Position,
x.LastPopulatedPosition,
x.ErrorCount,
x.LastError
});
}
}
}

0 comments on commit 97f4c96

Please sign in to comment.