RIA Services + Entity Framework 4 + POCO:'Timestamp字段是必需的'错误?

时间:2010-08-15 09:45:19

标签: entity-framework timestamp wcf-ria-services

我在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
)
;

时间戳的EDMX属性如下所示:(请注意,我只手动更改了红色属性)

alt text http://i35.tinypic.com/2ez7g9k.png

我使用t4模板自动生成POCO实体。 用户实体如下所示:

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;
    }

    ...
}

在ObjectContext上执行'SaveChanges'操作时,我得到一个名为的用户实体的验证错误:需要时间戳字段

2 个答案:

答案 0 :(得分:2)

解决方案:

我已将T4生成的User类更改为:(删除了'ConcurrencyCheck'属性)

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;
    }

    ...
}

我添加了一个通用元数据类,所有实体都使用它来排除时间戳属性:

/// <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; }
}

这解决了这个问题。

答案 1 :(得分:0)

一种选择是在EDMX模型中将Nullable属性设置为true,但在数据库中保留NOT NULL约束。

由于TimestampRowVersion)的生成类型是引用类型(byte[]),因此可以接受null值,因此不应破坏任何现有代码。