Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On Insert / Update logic in EF code first

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?

like image 231
jhoefnagels Avatar asked Aug 12 '11 14:08

jhoefnagels


People also ask

How do you update a stored procedure in code first in Entity Framework?

To use a Stored Procedure with the Code First model, we need to override the OnModelCreating method of DBContext and add the following code to map the Stored Procedure. The MapToStoreProcedures method has two overloaded methods, one method is without a parameter.

How do I add a new record in Entity Framework?

Use the DbSet. Add method to add a new entity to a context (instance of DbContext ), which will insert a new record in the database when you call the SaveChanges() method. In the above example, context.

How do I update a table in Entity Framework?

Update Objects in Entity Framework 4.0First retrieve an instance of the entity from the EntitySet<T> (in our case ObjectSet<Customer>), then edit the properties of the Entity and finally call SaveChanges() on the context.


2 Answers

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();
    }
like image 173
Eranga Avatar answered Sep 28 '22 03:09

Eranga


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.

like image 36
Ladislav Mrnka Avatar answered Sep 28 '22 01:09

Ladislav Mrnka