2010-08-15 20 views
3

我在MSSQL下表定義:RIA Services + Entity Framework 4 + POCO's:'時間戳字段是必需的'錯誤?

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」操作,我得到了用戶實體驗證錯誤,這就是所謂:需要時間戳字段

+0

您確定SaveChanges上的Timestamp字段中有值嗎? – 2010-08-15 10:22:14

+0

時間戳記字段應由SQL Server更新,而不是由RIA Client或Server代碼更新。我認爲在元數據中排除這個字段會起到訣竅的作用。我會告訴你這件事。 – 2010-08-15 11:26:35

+0

+1我遇到了完全相同的問題。 – 2011-04-04 08:53:57

回答

2

解決方案:

我已經改變了T4生成的用戶類:(除去 '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; } 
} 

這解決了問題。

+0

感謝您的回答,如何使用EntityMetaData類?您是否嘗試編輯t4模板文件? – 2011-04-04 08:58:08

0

一個選項是在EDMX模型中將Nullable屬性設置爲true,但在數據庫中保留NOT NULL約束。

至於TimestampRowVersion)生成的類型是引用類型(byte[]),因此可以接受null值,它不應該破壞任何現有的代碼。

相關問題