Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document configuration of triggers for SaveChanges #3883

Closed
ehsan12021 opened this issue Jun 6, 2022 · 10 comments · Fixed by #3887 or #4048
Closed

Document configuration of triggers for SaveChanges #3883

ehsan12021 opened this issue Jun 6, 2022 · 10 comments · Fixed by #3887 or #4048

Comments

@ehsan12021
Copy link

Hi,
I installed EF core version Preview 7 when I insert new record to a table that has a trigger I,ve got below error:

EF core preview 7 - latest preview release.

Error message:

Could not save changes because the target table has database triggers. Please configure your entity type accordingly, see https://aka.ms/efcore-docs-sqlserver-save-changes-and-triggers for more information.

thank you.

@ajcvickers
Copy link
Member

@ehsan12021 This is expected behavior. Do you have some specific question about it?

@ehsan12021
Copy link
Author

ehsan12021 commented Jun 6, 2022

Thank you for your response.
How can I prevent an error ??
Temporarily I had to go back to stable version.6.0.5

@ajcvickers
Copy link
Member

@roji Is this documented yet?

@roji
Copy link
Member

roji commented Jun 6, 2022

@ajcvickers no, I'll definitely prioritize this given that people are starting to run into it.

@roji
Copy link
Member

roji commented Jun 6, 2022

In the meantime, to resolve this, inform EF Core that the table in question has a trigger by calling HasTrigger():

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Blog>()
        .ToTable(tb => tb.HasTrigger("TriggerName"));
}

(the actual name doesn't matter at this point, since EF doesn't actually try to create the trigger - but that may change in the future)

@roji roji transferred this issue from dotnet/efcore Jun 6, 2022
@ehsan12021
Copy link
Author

Thank you.
Mr @roji
and
Mr @ajcvickers .

@roji roji reopened this Jun 7, 2022
@ajcvickers ajcvickers changed the title when insert new record to database , if table has a trigger the operation encountered an error. Document configuration of triggers for SaveChanges Jun 7, 2022
@ajcvickers ajcvickers added this to the 7.0.0 milestone Jun 7, 2022
roji added a commit to roji/EntityFramework.Docs that referenced this issue Jun 9, 2022
roji added a commit to roji/EntityFramework.Docs that referenced this issue Jun 9, 2022
@roji
Copy link
Member

roji commented Aug 17, 2022

Reopening to also add documentation for the custom convention which adds a blank trigger everywhere, dotnet/efcore#28749.

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Aug 19, 2022

        protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
        {
            configurationBuilder.Conventions.Add(_ => new BlankTriggerAddingConvention());
        }
public class BlankTriggerAddingConvention : 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");
                }
            }
        }
    }
}

ajcvickers added a commit that referenced this issue Sep 20, 2022
Fixes #3751
Fixes #4019
Fixes #3801
Fixes #3883
Part of #3915
Fixes #4010
Fixes #4029
Fixes #4039
Fixes #4047
ajcvickers added a commit that referenced this issue Sep 20, 2022
Fixes #3751
Fixes #4019
Fixes #3801
Fixes #3883
Part of #3915
Fixes #4010
Fixes #4029
Fixes #4039
Fixes #4047
ajcvickers added a commit that referenced this issue Sep 20, 2022
Fixes #3751
Fixes #4019
Fixes #3801
Fixes #3883
Part of #3915
Fixes #4010
Fixes #4029
Fixes #4039
Fixes #4047
@robcyrier11
Copy link

/// <summary>
///     Called when a model is being finalized.
/// </summary>
/// <param name="modelBuilder">The builder for the model.</param>
/// <param name="context">Additional information associated with convention execution.</param>
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.GetName(table.Value) == null))
        {
            entityType.Builder.HasTrigger(table.Value.Name + "_Trigger");
        }

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

}

Trying to implement this but seems things have changed - IConventionTrigger no longer implements GetName(StoreObjectIdentifier). @AndriySvyryd do you have an alternative? Thanks!

@roji
Copy link
Member

roji commented Dec 1, 2022

@robcyrier11 GetName has been renamed to GetDatabaseName - here are the docs for that convention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment