Home > Mobile >  Calculate NotMapped property when loading from EF Core
Calculate NotMapped property when loading from EF Core

Time:02-18

We do have an entity class defined as below:

[Table("Users", Schema = "Mstr")]
[Audited]
public class User
{
    public virtual string FamilyName { get; set; }

    public virtual string SurName { get; set; }
    
    [NotMapped]
    public virtual string DisplayName
    {
        get => SurName   " "   FamilyName;
        private set { }
    }
}

This is working just fine. Now we would like to extract the logic part SurName " " FamilyName to a helper class which is usually injected with dependency injection. Unfortunately DI is not working for an entity class.

Therefor my question: is there any way to intercept the creation of new User objects? Is there a method from EF which I could override to execute some additional logic after a User object was created by EF?

CodePudding user response:

Actually (at least in EF Core 6) you can use DI when constructing entities. Solution is a little bit hacky and based on the EF Core capability to inject "native" services like the context itself into entities constructors:

Currently, only services known by EF Core can be injected. Support for injecting application services is being considered for a future release.

And AccessorExtensions.GetService<TService> extension method which seems to support resolving services from DI.

So basically just introduce ctor accepting your DbContext as a parameter to the entity and call GetService on it and use service:

public class MyEntity
{
    public MyEntity()
    {        
    }

    public MyEntity(SomeContext context)
    {
        var valueProvider = context.GetService<IValueProvider>();
        NotMapped = valueProvider.GetValue();
    }

    public int Id { get; set; }

    [NotMapped]
    public string NotMapped { get; set; }
}


// Example value provider:
public interface IValueProvider
{
    string GetValue();
}

class ValueProvider : IValueProvider
{
    public string GetValue() => "From DI";
}

Example context:

public class SomeContext : DbContext
{
    public SomeContext(DbContextOptions<SomeContext> options) : base(options)
    {
    }

    public DbSet<MyEntity> Entities { get; set; }
}

And example:

var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<IValueProvider, ValueProvider>();
serviceCollection.AddDbContext<SomeContext>(builder => 
    builder.UseSqlite($"Filename={nameof(SomeContext)}.db"));
var serviceProvider = serviceCollection.BuildServiceProvider();
// init db and add one item
using (var scope = serviceProvider.CreateScope())
{
    var someContext = scope.ServiceProvider.GetRequiredService<SomeContext>();
    someContext.Database.EnsureDeleted();
    someContext.Database.EnsureCreated();
    someContext.Add(new MyEntity());
    someContext.SaveChanges();
}

// check that value provider is used
using (var scope = serviceProvider.CreateScope())
{
    var someContext = scope.ServiceProvider.GetRequiredService<SomeContext>();
    var myEntities = someContext.Entities.ToList();
    Console.WriteLine(myEntities.First().NotMapped); // prints "From DI"
}

Note that var valueProvider = context.GetService<IValueProvider>(); will throw if service is not registered so possibly next implementation is better:

public MyEntity(SomeContext context)
{
    var serviceProvider = context.GetService<IServiceProvider>();
    var valueProvider = serviceProvider.GetService<IValueProvider>();
    NotMapped = valueProvider?.GetValue() ?? "No Provider";
}

Also you can consider removing not mapped property and creating separate model with it and service which will perform the mapping.

CodePudding user response:

In your DbContext or whatever your context file is called you can intercept the SaveChanges() method and override it with your own things. In my example I override SaveChanges() to automatically add my audit fields so I don't have to duplicate it all over the code in a million places.

here is my example. So when a new object is being created you can override it. In My example I override both New records Added and Records modified.

These are notated at EntitState.Added and EntityStateModified.

Here is the code.

public override int SaveChanges()
        {

            var state = this.ChangeTracker.Entries().Select(x => x.State).ToList();

            state.ForEach(x => {
                if (x == EntityState.Added)
                {
                    //Create new record changes
                    var created = this.ChangeTracker.Entries().Where(e => e.State == EntityState.Added).Select(e => e.Entity).ToArray();

                    foreach (var entity in created)
                    {
                        if (entity is AuditFields)
                        {
                            var auditFields = entity as AuditFields;
                            auditFields.CreateDateTimeUtc = DateTime.UtcNow;
                            auditFields.ModifiedDateTimeUtc = DateTime.UtcNow;
                            auditFields.Active = true;
                        }
                    }
                }

                else if (x == EntityState.Modified)
                {
                    //Modified record changes
                    var modified = this.ChangeTracker.Entries().Where(e => e.State == EntityState.Modified).Select(e => e.Entity).ToArray();

                    foreach (var entity in modified)
                    {
                        if (entity is AuditFields)
                        {
                            var auditFields = entity as AuditFields;
                            auditFields.ModifiedDateTimeUtc = DateTime.UtcNow;
                        }
                    }

                }
                else
                {
                    //do nothing
                }

            });

            return base.SaveChanges();
        }

Since you said:

is there any way to intercept the creation of new User objects?

You would want to do your logic in the EntityState.Added area of code above and this will allow you to intercept the creation of your new User and do whatever you want to do before it is saved to Database.

  • Related