Home / Community & Support / Knowledge Base / Telerik OpenAccess ORM / General / Handling the ‘Explicit change tracking required for the array field…’ warning

Handling the ‘Explicit change tracking required for the array field…’ warning

Article Info

Rating: Not rated

Article information

Article relates to

 Telerik OpenAccess ORM

Created by

 Alexander Filipov

Last modified

 April 9, 2012

Last modified by

 Pencho Popadiyn


DESCRIPTION

By default the OpenAccessContext silently manages the change tracking of the persistent objects. However, this is not possible for all types of fields that a class may contain. For example, changes in array fields could not be fully tracked by the object context. This limitation is present when an array field is not completely replaced with another array but only a certain element is changed. To notify the programmer for this behavior, Telerik OpenAccess ORM shows a warning at compile time with this limitation:
OpenAccess Warning: Explicit change tracking required for the array field Namespace.ClassName.FieldName

SOLUTION

When you change some value of an array, you will have to inform the context that you have made a changes, so that it can be persisted to the database.
The code below would not persist the change in the byte[] field named Picture from the Category object:

using (EntitiesModel dbContext = new EntitiesModel())
{
    Category category = dbContext.Categories.FirstOrDefault();
    category.Picture[1] = 0;
    dbContext.SaveChanges();
}

As the warning states, explicit change tracking is required in this case. This can be accomplished by using the OpenAccessContext.MakeDirty method. It marks a field as “dirty”, thereby the field value is forced to be stored in the database. Below is the same sample code that modifies one of the array elements but this time the change is persisted.

using (EntitiesModel dbContext = new EntitiesModel())
{
    Category category = dbContext.Categories.FirstOrDefault();
    category.Picture[1] = 0;
    dbContext.MakeDirty(category, "Picture");
    dbContext.SaveChanges();
}

The case when the whole array is replaced with another array does not require any additional operations. The change tracking is done internally:

using (EntitiesModel dbContext = new EntitiesModel())
{
    Category category = dbContext.Categories.FirstOrDefault();
    category.Picture = new byte[] { 0, 1, 2 };               
    dbContext.SaveChanges();
}

Comments

There are no comments yet.
If you'd like to comment on this KB article, please, send us a Support Ticket.
Thank you!

Please Sign In to rate this article.