【问题标题】:RIA Services + Entity Framework 4 + POCO's : 'The Timestamp field is required' error?RIA 服务 + 实体框架 4 + POCO:“需要时间戳字段”错误?
【发布时间】:2010-08-15 09:45:18
【问题描述】:

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

Timestamp 的 EDMX 属性如下所示:(注意只有红色属性是我手动更改的)

我使用 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”操作时,我收到名为 User 实体的验证错误:Timestamp field is required

【问题讨论】:

  • 您确定 SaveChanges 的 Timestamp 字段中有值吗?
  • 时间戳字段应该由 SQL 服务器更新,而不是由 RIA 客户端或服务器代码更新。我认为在元数据中排除这个字段就可以了。我会及时通知您。
  • +1 我遇到了完全相同的问题。

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


【解决方案1】:

解决方案:

我已将 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;
    }

    ...
}

我添加了一个通用元数据类,所有实体都使用它,不包括 Timestamp 属性:

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

这解决了问题。

【讨论】:

  • 谢谢解答,EntityMetaData类是怎么用的?您是否尝试过编辑 t4 模板文件?
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多