2010-08-15 1 views
3

나는 MSSQL에서 다음 테이블 정의했습니다 :RIA 서비스 + Entity Framework 4 + POCO : '타임 스탬프 필드가 필요합니다'오류가 발생 했습니까?

타임 스탬프의 EDMX 속성은 다음과 같습니다
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 
) 
; 

는 :

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를'작업을하고, 나는 호출되는 사용자 개체에 대한 유효성 검사 오류를 얻을 : 명 Timestamp 필드가 필요합니다

+0

SaveChanges의 Timestamp 필드에 값이 있습니까? –

+0

타임 스탬프 필드는 RIA 클라이언트 또는 서버 코드가 아닌 SQL 서버에 의해 업데이트되어야합니다. 메타 데이터에서이 필드를 제외하면 트릭을 수행 할 것입니다. 이 사실을 알려 드리겠습니다. –

+0

+1 똑같은 문제가 발생했습니다. –

답변

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 템플릿 파일을 편집 해 보셨습니까? –

0

하나의 옵션은 EDMX 모델에서 으로 Nullable 속성을 설정하지만 데이터베이스에는 NOT NULL 제약 조건을 유지하는 것입니다. Timestamp (RowVersion)에 대해 생성 타입으로서

null 값을 수용 할 따라서 참조 타입 (byte[])이고, 그것은 기존 코드를 파괴해서는 안된다.