3

When I run this code I get an exception at the Update method

public void UpdateTeststep(Teststep step)
        {
            _context.Teststeps.Where(t => t.TeststepId == step.TeststepId).Update(t => step);
            _context.SaveChanges();
        }

{"The update expression must be of type MemberInitExpression.\r\nParametername: updateExpression"}

What is wrong with my updateExpression?

Thats the source code of the Update method:

http://www.symbolsource.org/Public/Metadata/NuGet/Project/EntityFramework.Extended/1.0.0.20/Release/.NETFramework,Version%3Dv4.0/EntityFramework.Extended/EntityFramework.Extended/EntityFramework.Extended/Extensions/BatchExtensions.cs?ImageName=EntityFramework.Extended

Line 454:

var memberInitExpression = updateExpression.Body as MemberInitExpression;
                if (memberInitExpression == null)
                    throw new ArgumentException("The update expression must be of type MemberInitExpression.", "updateExpression");

Why is the value I pass null? Do I pass my teststep in the wrong way?

Pascal
  • 12,265
  • 25
  • 103
  • 195
  • 2
    Yes, the exception tells you the update expression must be of type MemberInitExpression, but it isn't. Do you know what a MemberInitExpression is? If not, then it would have been a better question to just ask that. –  Apr 24 '14 at 09:41
  • I have just read about MemberInitExpression: Update(s => new Teststep { Id = step.Id}); Is this not correct? at least it does not work! – Pascal Apr 24 '14 at 09:52
  • 1
    That is indeed the syntax that corresponds to a `MemberInitExpression`, so that's a step closer. However, this only specifies that it updates `Id` to `step.Id` -- but `Id` is already `step.Id`, because that's the only thing you selected in your `Where` selector. –  Apr 24 '14 at 10:32
  • It seems to me you know how to write it, why do you tease me? I am still getting the exception with my corresponding MemberInitExpression. – Pascal Apr 24 '14 at 11:18
  • 1
    That's not what I'm trying to do. I haven't used EntityFramework.Extended, it's just that even without having used it, the exception seems clear to me, and I wouldn't expect your replacement to work either. If I had an actual answer, I would've posted it. As it is, I was trying to help you find the answer yourself. –  Apr 24 '14 at 12:00

2 Answers2

5

You are giving Update a MemberExpression instead of a MemberInit expression and this won't tell you anything useful if you haven't dealt with writing and parsing expressions before. However, this exception is telling you that you have to use a code that initializes a new object's properties.

Update(t => step) // member expression because step is a variable
Update(t=> new Something{ Step=step}) // member init expression 

Simply put, you need to always instantiate some object with at least a property in order to work.

MikeSW
  • 16,140
  • 3
  • 39
  • 53
0

Lets say there is an entity Person with a corresponding set Persons on a DbContext derived class TestDbContext:

public class Person
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }
}

Then a batch update using EntityFrameworkExtended can be written like this:

[TestMethod]
public void UpdatePersonName()
{
    using (var context = new TestDbContext())
    {
        // insert 'initial' person to be renamed
        context.Persons.Add(new Person {Name = "andyp"});
        context.SaveChanges();

        Assert.AreEqual(1, context.Persons.Count());
        Assert.AreEqual("andyp", context.Persons.Select(p => p.Name).Single());

        // update the persons name
        context.Persons
               .Where(p => p.Id == 1)
               .Update(p => new Person {Name = "Pascal"});

        // assert that the update has been successful
        Assert.AreEqual(1, context.Persons.Count());
        Assert.AreEqual("Pascal", context.Persons.Select(p => p.Name).Single());
    }
}

Notice that I'm updating only one entity (Id == 1) but a where condition selecting more than one entity is of course valid as well. The update expression also allows to change more than one property at once.

andyp
  • 6,229
  • 3
  • 38
  • 55