Search code examples
entity-frameworktimestampwcf-ria-services

RIA Services + Entity Framework 4 + POCO's : 'The Timestamp field is required' error?


I've the following table definition in MSSQL:

CREATE TABLE [User] ( 
    [Id] bigint identity(1,1)  NOT NULL,
    [Email] nvarchar(256),
    [PasswordHash] nvarchar(128) NOT NULL,
    [PasswordFormat] int DEFAULT ((0)) NOT NULL,
    [PasswordSalt] nvarchar(10) NOT NULL,
    [Timestamp] timestamp
)
;

The EDMX property for Timestamp looks like this: (Note only the red property has been manually changed by me)

alt text

I used the t4 template to automatically generate POCO entities. The User entity looks like this:

public partial class User : IEntity
{
    public virtual long Id
    {
        get;
        set;
    }
    ...

    [TimestampAttribute]
    [ConcurrencyCheck]
    [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Justification = "Autogenerated by T4.")]
    public virtual byte[] Timestamp
    {
        get;
        set;
    }

    ...
}

When doing a 'SaveChanges' operation on the ObjectContext, I get a validation error for the User entity which is called : The Timestamp field is required


Solution

  • Solution:

    I've changed the T4 generated User class to: (removed the 'ConcurrencyCheck' attribute)

    public partial class User : IEntity
    {
        public virtual long Id
        {
            get;
            set;
        }
        ...
    
        [TimestampAttribute]
        [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Justification = "Autogenerated by T4.")]
        public virtual byte[] Timestamp
        {
            get;
            set;
        }
    
        ...
    }
    

    And I've added a generic metadata class which is used by all Entities which excludes the Timestamp property :

    /// <summary>
    /// A MetaData which defines some default metadata for an Entity
    /// </summary>
    public class EntityMetaData
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="EntityMetaData"/> class.
        /// </summary>
        protected EntityMetaData()
        {
        }
    
        /// <summary>
        /// Gets or sets the timestamp.
        /// Note : this field is excluded on the client.
        /// </summary>
        /// <value>The timestamp.</value>
        [Exclude]
        public byte[] Timestamp { get; set; }
    }
    

    This solves the issue.