Skip to content

Commit

Permalink
add specific EF core triggers condition for .net 7
Browse files Browse the repository at this point in the history
  • Loading branch information
Mimetis committed Nov 30, 2022
1 parent 4520998 commit 9ba75d7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,30 @@ public abstract partial class BaseOrchestrator
internal virtual async Task<(SyncContext context, bool provisioned)> InternalProvisionAsync(ScopeInfo scopeInfo, SyncContext context, bool overwrite, SyncProvision provision,
DbConnection connection, DbTransaction transaction, CancellationToken cancellationToken, IProgress<ProgressArgs> progress)
{
try
{

if (Provider == null)
throw new MissingProviderException(nameof(InternalProvisionAsync));
if (Provider == null)
throw new MissingProviderException(nameof(InternalProvisionAsync));

context.SyncStage = SyncStage.Provisioning;
context.SyncStage = SyncStage.Provisioning;

// If schema does not have any table, raise an exception
if (scopeInfo.Schema == null || scopeInfo.Schema.Tables == null || !scopeInfo.Schema.HasTables)
throw new MissingTablesException();
// If schema does not have any table, raise an exception
if (scopeInfo.Schema == null || scopeInfo.Schema.Tables == null || !scopeInfo.Schema.HasTables)
throw new MissingTablesException();

await this.InterceptAsync(new ProvisioningArgs(context, provision, scopeInfo.Schema, connection, transaction), progress, cancellationToken).ConfigureAwait(false);
await this.InterceptAsync(new ProvisioningArgs(context, provision, scopeInfo.Schema, connection, transaction), progress, cancellationToken).ConfigureAwait(false);

try
{
// get Database builder
var builder = this.Provider.GetDatabaseBuilder();

// Initialize database if needed
await builder.EnsureDatabaseAsync(connection, transaction).ConfigureAwait(false);
}
catch (Exception ex)
{
throw GetSyncError(context, ex, "Error during EnsureDatabaseAsync");
}

try
{
// Check if we have tables AND columns
// If we don't have any columns it's most probably because user called method with the Setup only
// So far we have only tables names, it's enough to get the schema
Expand Down Expand Up @@ -124,7 +128,6 @@ public abstract partial class BaseOrchestrator
{
try
{

if (Provider == null)
throw new MissingProviderException(nameof(InternalDeprovisionAsync));

Expand Down
8 changes: 8 additions & 0 deletions Tests/Dotmim.Sync.Tests/Models/AdventureWorksContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public AdventureWorksContext()
{
}


protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
#if NET7_0
configurationBuilder.Conventions.Add(_ => new AdventureWorksTriggerAddingConvention());
#endif
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using System.Linq;

namespace Dotmim.Sync.Tests.Models
{
#if NET7_0
public class AdventureWorksTriggerAddingConvention : IModelFinalizingConvention
{
public virtual void ProcessModelFinalizing(
IConventionModelBuilder modelBuilder,
IConventionContext<IConventionModelBuilder> context)
{
foreach (var entityType in modelBuilder.Metadata.GetEntityTypes())
{
var table = StoreObjectIdentifier.Create(entityType, StoreObjectType.Table);
if (table != null
&& entityType.GetDeclaredTriggers().All(t => t.GetDatabaseName(table.Value) == null))
{
entityType.Builder.HasTrigger(table.Value.Name + "_Trigger");
}

foreach (var fragment in entityType.GetMappingFragments(StoreObjectType.Table))
{
if (entityType.GetDeclaredTriggers().All(t => t.GetDatabaseName(fragment.StoreObject) == null))
{
entityType.Builder.HasTrigger(fragment.StoreObject.Name + "_Trigger");
}
}
}
}
}
#endif
}

0 comments on commit 9ba75d7

Please sign in to comment.