15

I would like to add some logic to the insert and update events of some EF objects. I have a MVC application with category object which has a property which is a slugified version of the name property.

public class Category
{

    public string Name { get; set; }
    public string UrlName{ get; set; }
}

I would like to set the UrlName property only on the insert and update events because my slugify logic is quite elaborate.

I am aware that I can add some logic inside the SaveChanges() function on the context itself but I rather would like to put the code closer to the entity itself.

Is there a way to accomplish such thing using EF code first?

2 Answers 2

29

You can setup a base class with methods to be called before insert and update

public abstract class Entity
{
    public virtual void OnBeforeInsert(){}
    public virtual void OnBeforeUpdate(){}
}

public class Category : Entity
{

    public string Name { get; set; }
    public string UrlName{ get; set; }

    public override void OnBeforeInsert()
    {
       //ur logic
    }
}

Then in your DbContext

    public override int SaveChanges()
    {
        var changedEntities = ChangeTracker.Entries();

        foreach (var changedEntity in changedEntities)
        {
            if (changedEntity.Entity is Entity)
            {
                var entity = (Entity)changedEntity.Entity;

                switch (changedEntity.State)
                {
                    case EntityState.Added:
                        entity.OnBeforeInsert();
                        break;

                    case EntityState.Modified:
                        entity.OnBeforeUpdate();
                        break;

                }
            }
        }

        return base.SaveChanges();
    }
4
  • 1
    This is a bad solution as it forces inheritance. Better way would be to use Interface.
    – Migol
    Apr 12, 2012 at 15:28
  • 1
    @Migol answer illustrate how you can implement a callback mechanism. How you implement it is upto you.
    – Eranga
    Apr 12, 2012 at 15:42
  • 2
    @Migol, his answer is good because using interfaces forces implementation of the methods.
    – orourkedd
    Apr 12, 2013 at 22:04
  • Thanks a lot, it's by far the cleanest approach I seen regarding this scenario and I successfully applied it where I need to update a numeric concurrency check value before updating and inserting in a central place. Thanks again. Feb 27, 2014 at 15:26
3

No there is no such extension point because your entity is POCO - it is not aware of its persistence. Such logic must be triggered in data access layer which is aware of persistence. DbContext API offers only overriding of SaveChanges.

You can expose custom events or methods on your entities and call them during processing in SaveChanges.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.