Reputation: 14259
I am trying to update a list of tags in a Tag table like the following:
List<Tag> tagList = GetUnitTagList(unitId);
foreach (Tag tag in tagList)
{
tag.count = tag.count - 1;
}
db.SubmitChanges();
and here is the GetUnitTagList() method:
public static List<Tag> GetUnitTagList(int unitId)
{
DataClassesDataContext db = new DataClassesDataContext();
var tags = from x in db.UnitTags where x.UnitID == unitId select x.Tag;
return tags.ToList();
}
But the value I am trying to update does not get updated.
However, the following code works just fine:
var tags = from x in db.UnitTags where x.UnitID == unitId select x.Tag;
foreach (Tag tag in tags)
{
tag.count = tag.count - 1;
}
db.SubmitChanges();
It appears like if I get a collection of Tag objects us a method return then I am not able to update the record. I tried to debug and nothing through an exception!
So, Why is the first way of updating the records is not working while the second way it just work with no problem?
Upvotes: 0
Views: 158
Reputation: 5832
You're updating entities from one dbContext (created in GetUnitTagList
) and calling SubmitChanges
on another (created somewhere you don't show us).
In your last example dbcontext is obviously one and same.
Upvotes: 5