17

I am working on mvc project, with repository pattern and entity framework, now on my form i have a sample model

SampleModel
1) name
2) age
3) address
4) notes
5) date updated

I am displaying only following data on the edit form
1) name
2) age
3) address

now if i update the model with missing property values using the repository, the notes, dateupdated field goes null.

My question is how do i update only few selected properties using the repository ( tryupdatemodel not available in repository ) and i dont want to call the original object and map the properites with the updated model.

Is there any way, there must be.

tereško
  • 58,060
  • 25
  • 98
  • 150
Milan
  • 203
  • 1
  • 2
  • 7

2 Answers2

24

You can update only subset of fields:

using (var context = new YourDbContext())
{
    context.SamepleModels.Attach(sampleModel);

    DbEntityEntry<SameplModel> entry = context.Entry(sampleModel);
    entry.Property(e => e.Name).IsModified = true;
    entry.Property(e => e.Age).IsModified = true;
    entry.Property(e => e.Address).IsModified = true;   

    context.SaveChanges();
}

or in ObjectContext API:

using (var context = new YourObjectContext())
{
    context.SamepleModels.Attach(sampleModel);

    ObjectStateEntry entry = context.ObjectStateManager.GetObjectStateEntry(sampleModel);
    entry.SetModifiedProperty("Name");
    entry.SetModifiedProperty("Age");
    entry.SetModifiedProperty("Address"); 

    context.SaveChanges();
}
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • ohh ok or write sql query to update, right? or write a generic helper to mark the edited properties, humm... thats what TryUpdate must be doing. – Milan Mar 22 '12 at 11:12
  • Entry() doesn't exist in EF 4.0. – Suncat2000 May 22 '13 at 16:01
  • 1
    @Suncat2000: The second example is for EF 4.0, the first one is for EF 4.1 and newer. – Ladislav Mrnka May 22 '13 at 18:45
  • 1
    How to change this method for updating different properties without recompilation ? – BILL Jun 22 '14 at 14:33
  • 1
    I would just add that to get the best out of this approach, you're going to have to disable model validation at the context level via .Configuration.ValidateOnSave = false. Technically you can just make sure your model satisfies the model state validation by putting in 'dummy data' in your model - this won't get sent to SQL if you don't set those properties to modified, but I would recommend against that approach. – Forest Aug 08 '15 at 13:36
3

This is an old thread, but if anyone is interested, to extend on Ladislav's solutions, we've come up with a helpful extension method for EF 4.1 and newer:

public static void SetModified<TEntity>(
        this DbEntityEntry<TEntity> entry,
        IEnumerable<Expression<Func<TEntity, object>>> expressions) where TEntity : class, IEntity
    {
        foreach (var expression in expressions)
            entry.Property(expression).IsModified = true;
    }

Obviously you'll need to take away the IEntity constraint unless you're using an interface by the same name for your POCOs.

Example usage would be:

        var user = new User
        {
            Id = Request.Id,
            UserName = Request.UserName,
            FirstName = Request.FirstName
        };

        var expressions = new List<Expression<Func<User, object>>> 
                 { 
                     x => x.UserName, 
                     x => x.FirstName
                 };

        context.Entry(user).SetModified(expressions);
Forest
  • 149
  • 1
  • 7