ReadOnly Fields Being Updated

2 posts, 0 answers
  1. Tim
    Tim avatar
    5 posts
    Member since:
    Feb 2013

    Posted 13 Mar 2013 Link to this post

    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?
  2. Yordan
    Admin
    Yordan avatar
    39 posts

    Posted 18 Mar 2013 Link to this post

    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.
  3. DevCraft banner
Back to Top