This is a migrated thread and some comments may be shown as answers.

ReadOnly Fields Being Updated

1 Answer 49 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Tim
Top achievements
Rank 1
Tim asked on 13 Mar 2013, 06:53 PM
I am using ORM 2013.1.219.3 and ASP.NET Web API with Kendo UI.

In my Entities model I have table called "Assets" with a column called "CreatedDate".  The column value is set by a Default Constraint "GetDate()" added to the table.  In the model I set the fields's Kind value to PersistentReadOnly and the resulting class has the correct property defined:

private DateTime _createdDate;
public virtual DateTime CreatedDate
{
  get
   {
      return this._createdDate;
   }
}

I can get the value and display it correctly on a web page when editing the record.  The problem I am having is when I click Save to submit my changes an error is thrown on this line "TEntity attachedEntity = dataContext.AttachCopy(entity);" in the OpanAccessBaseRepository class.  The error message is "The update of field 'AssetTracking.DataLayer.Asset._createdDate' is not allowed."

public virtual TEntity Update(TEntity entity)
        {
            if (entity == null)
                throw new ArgumentNullException("entity");
 
            TEntity attachedEntity = dataContext.AttachCopy(entity);
            dataContext.SaveChanges();
            TEntity detachedEntity = dataContext.CreateDetachedCopy(attachedEntity, fetchStrategy);
 
            return detachedEntity;
        }

How do I indicate to OpenAccess that the field should not be included in the update?

1 Answer, 1 is accepted

Sort by
0
Yordan
Telerik team
answered on 18 Mar 2013, 11:48 AM
Hello Tim,

As we see from the provided code you have a column in the database that is set by the default constraint GetDate(). We have a short article that explains what should be done In this situation. Generally you have to execute two steps. The first is that you have to set the property HasDefaultValue of the column CreateDate to True in the Model Schema Explorer. The second is to ensure that the property Nullable for CreateDate column is set to True.

After this setup, the kind property of the CreateDate column should not be set to PersistentReadOnly - this is why the update statement throws an exception. The kind property should remain Persistent. 

If you still have any issues using the OpenAccess Web API please get back to us for assistance.

Regards,
Yordan
the Telerik team
OpenAccess ORM Q1 2013 is out featuring Multi-Diagrams, Persistent Data Stream Support and much more. Check out all of the latest highlights.
Tags
Development (API, general questions)
Asked by
Tim
Top achievements
Rank 1
Answers by
Yordan
Telerik team
Share this question
or