-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Comments
@ehsan12021 This is expected behavior. Do you have some specific question about it? |
Thank you for your response. |
@roji Is this documented yet? |
@ajcvickers no, I'll definitely prioritize this given that people are starting to run into it. |
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) |
Thank you. |
Reopening to also add documentation for the custom convention which adds a blank trigger everywhere, dotnet/efcore#28749. |
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");
}
}
}
}
} |
Trying to implement this but seems things have changed - IConventionTrigger no longer implements GetName(StoreObjectIdentifier). @AndriySvyryd do you have an alternative? Thanks! |
@robcyrier11 GetName has been renamed to GetDatabaseName - here are the docs for that convention. |
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.
The text was updated successfully, but these errors were encountered: