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

DbCommandInterceptor to set entity properties #21877

Closed
R4ZuL opened this issue Jul 31, 2020 · 1 comment
Closed

DbCommandInterceptor to set entity properties #21877

R4ZuL opened this issue Jul 31, 2020 · 1 comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@R4ZuL
Copy link

R4ZuL commented Jul 31, 2020

Hi,

i have a customer entity that includes CreatedBy, CreatedAt, ModifiedBy and ModifiedAt properties.

public class CustomerEntity : IIdEntity<int>, IAuditableEntity
{
        public int Id { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }

        public string CreatedBy { get; set; }

        public string ModifiedBy { get; set; }

        public DateTime CreatedAt { get; set; }

        public DateTime ModifiedAt { get; set; }
}

What i try to achieve is to set the values for the above mentioned properties at runtime by using one of the provided DbCommandInterceptor functions. I already tried to implement something like this:

public override InterceptionResult<DbCommand> CommandCreating(
            CommandCorrelatedEventData eventData, InterceptionResult<DbCommand> result)
{
            var dbContext = eventData.Context;
            var entries = dbContext.ChangeTracker.Entries().Where(x =>
                x.Entity is IAuditableEntity && (x.State == EntityState.Added || x.State == EntityState.Modified));

            foreach (var entry in entries)
            {
                var now = DateTime.Now;
                var entity = (IAuditableEntity)entry.Entity;

                if (entry.State == EntityState.Added)
                {
                    entity.CreatedAt = now;
                    entity.CreatedBy = "testUser";
                }

                entity.ModifiedAt = now;
                entity.ModifiedBy = "testUser";
            }

            return result;
}

As you can imagine this didn't work. Can you provide me any solution ?

Further technical details

EF Core version: Microsoft.EntityFrameworkCore 3.1.4, Microsoft.EntityFrameworkCore.Relational 3.1.4
Database provider: Microsoft.EntityFrameworkCore.SqlServer (Productive), Microsoft.EntityFrameworkCore.Sqlite (Testing)
Target framework: : .NETCore 3.1
Operating system: Microsoft Windows 10
IDE: Visual Studio 2019 16.6.3

@ajcvickers
Copy link
Member

@R4ZuL A command interceptor is likely not the right approach for this, since you're not to be able to change which properties to save at this point. Typically this is done by overriding SaveChanges. In EF Core 5.0, the SaveChanges interceptor or event could be used instead.

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Aug 3, 2020
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

2 participants