开发者

LINQ to SQL Field Won't Update (Sometimes)

I have a MVC Web Application using the following approach:

public class MyController : Controll开发者_开发百科er
{
    public FooRepository fooRepository = new FooRepository();
    public BarRepository barRepository = new BarRepository();

    public ActionResult UpdateItems(int id, int range1, int range2)
    {       
        Foo foo = fooRepository.GetItem(id);
        List<Bar> bars = barRepository.GetItemsByRange(range1, range2);

        // Some validation rules here...

        DoSomeWork(foo, bars);

        // Show confirmation / error message
    }

    private void DoSomeWork(Foo foo, List<Bar> bars)
    {
        foreach(int i = 0; i < bars.Count; i++)
        {
            bars[i].Prop1 = foo.Prop1; // This field is updated
            bars[i].Owner = "someuser"; // This one too
            bars[i].Status = BarStatus.SomeStatus; // This isn't...
        }
        foo.Status = FooStatus.SomeStatus; // Ok

        // Calls DataContext.SubmitChanges()
        fooRepository.SubmitChanges();
        barRepository.SubmitChanges();      
    }
}

However, in some "random" cases (I see no pattern), one of the fields doesn't get updated, as noted in the comments.

It seems like LINQ isn't recognizing the field's update, so it gets excluded from the generated query.

Can anyone tell me if I'm missing something here, what could be causing it and/or how can I solve it?

Note: I don't get any Exception and can't verify this case in a development scenario.


From my experience if the error is random and you can't reproduce in development than the problem is user error.

Programming would be really hard if the .net framework or the CLR just randomly decided to do things differently.


You probably have an implicit/explicit bind exclusion floating around somewhere

[Bind(Exclude="...,Status,...")]

Just guessing of course


If Linq thinks that the Status is already BarStatus.SomeStatus, then it won't update it.

What can happen is that you find a record with the status set to this value, and then some other routine changes it, and then, if you are using your same DataContext, you will get the old value from the cached copy and hence Linq thinks that it does not need to update it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜