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
19 changes: 8 additions & 11 deletions src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,13 @@ public virtual Task<bool> EnsureCreatedAsync(CancellationToken cancellationToken

var created = new StrongBox<bool>(false);
var dataInserted = new StrongBox<bool>(false);
var retrying = new StrongBox<bool>(false);
var seeded = new StrongBox<bool>(false);
return _executionStrategy.ExecuteAsync(
(Creator: this, Created: created, DataInserted: dataInserted, Retrying: retrying),
(Creator: this, Created: created, DataInserted: dataInserted, Seeded: seeded),
static async (_, state, ct) =>
{
var creator = state.Creator;

if (state.Retrying.Value)
{
creator._currentContext.Context.ChangeTracker.Clear();
}

state.Retrying.Value = true;

if (!state.DataInserted.Value)
{
var model = creator._designTimeModel.Model;
Expand All @@ -101,8 +94,12 @@ static async (_, state, ct) =>
}
}

await creator.SeedDataAsync(state.Created.Value, cancellationToken: ct)
.ConfigureAwait(false);
if (!state.Seeded.Value)
{
await creator.SeedDataAsync(state.Created.Value, cancellationToken: ct)
.ConfigureAwait(false);
state.Seeded.Value = true;
}

return state.Created.Value;
}, verifySucceeded: null, cancellationToken);
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Relational/EFCore.Relational.baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -7921,7 +7921,7 @@
"Member": "int LastCommittedCommandIndex { get; set; }"
},
{
"Member": "bool SeedingAttempted { get; set; }"
"Member": "bool SeedingCompleted { get; set; }"
},
{
"Member": "Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction? Transaction { get; set; }"
Expand Down
16 changes: 6 additions & 10 deletions src/EFCore.Relational/Migrations/Internal/Migrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,11 @@ private bool MigrateImplementation(
var seed = coreOptionsExtension.Seeder;
if (seed != null)
{
if (state.SeedingAttempted)
if (!state.SeedingCompleted)
{
context.ChangeTracker.Clear();
seed(context, state.AnyOperationPerformed);
state.SeedingCompleted = true;
}
Comment thread
AndriySvyryd marked this conversation as resolved.

state.SeedingAttempted = true;
seed(context, state.AnyOperationPerformed);
}
else if (coreOptionsExtension.AsyncSeeder != null)
{
Expand Down Expand Up @@ -334,13 +332,11 @@ await _migrationCommandExecutor.ExecuteNonQueryAsync(
var seedAsync = coreOptionsExtension.AsyncSeeder;
if (seedAsync != null)
{
if (state.SeedingAttempted)
if (!state.SeedingCompleted)
{
context.ChangeTracker.Clear();
await seedAsync(context, state.AnyOperationPerformed, cancellationToken).ConfigureAwait(false);
state.SeedingCompleted = true;
}

state.SeedingAttempted = true;
await seedAsync(context, state.AnyOperationPerformed, cancellationToken).ConfigureAwait(false);
}
else if (coreOptionsExtension.Seeder != null)
{
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Relational/Migrations/MigrationExecutionState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public sealed class MigrationExecutionState
public IDbContextTransaction? Transaction { get; set; }

/// <summary>
/// Indicates whether seeding has been attempted.
/// Indicates whether seeding has been completed.
/// </summary>
public bool SeedingAttempted { get; set; }
public bool SeedingCompleted { get; set; }
}
20 changes: 10 additions & 10 deletions src/EFCore.Relational/Storage/RelationalDatabaseCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,14 @@ public virtual bool EnsureCreated()
}

return Dependencies.ExecutionStrategy.Execute(
(Creator: this, Created: operationsPerformed, Retrying: new StrongBox<bool>(false)),
(Creator: this, Created: operationsPerformed, Seeded: new StrongBox<bool>(false)),
static (context, state) =>
{
if (state.Retrying.Value)
if (state.Seeded.Value)
{
context.ChangeTracker.Clear();
return state.Created;
}

state.Retrying.Value = true;

var coreOptionsExtension =
state.Creator.Dependencies.ContextOptions.FindExtension<CoreOptionsExtension>();

Expand All @@ -283,6 +281,8 @@ public virtual bool EnsureCreated()
throw new InvalidOperationException(CoreStrings.MissingSeeder);
}

state.Seeded.Value = true;

return state.Created;
}, verifySucceeded: null);
}
Expand Down Expand Up @@ -324,16 +324,14 @@ public virtual async Task<bool> EnsureCreatedAsync(CancellationToken cancellatio
}

return await Dependencies.ExecutionStrategy.ExecuteAsync(
(Creator: this, Created: operationsPerformed, Retrying: new StrongBox<bool>(false)),
(Creator: this, Created: operationsPerformed, Seeded: new StrongBox<bool>(false)),
static async (context, state, ct) =>
{
if (state.Retrying.Value)
if (state.Seeded.Value)
{
context.ChangeTracker.Clear();
return state.Created;
}

state.Retrying.Value = true;

var coreOptionsExtension =
state.Creator.Dependencies.ContextOptions.FindExtension<CoreOptionsExtension>();

Expand All @@ -350,6 +348,8 @@ static async (context, state, ct) =>
throw new InvalidOperationException(CoreStrings.MissingSeeder);
}

state.Seeded.Value = true;

return state.Created;
}, verifySucceeded: null, cancellationToken).ConfigureAwait(false);
}
Expand Down
Loading