2012-01-26 86 views
1

我正在使用RIA Services和EF Code First創建一個應用程序,其中實體可以使用自定義屬性進行擴展(每個可擴展實體都有一個基本上是一對多關係的屬性包與財產實體)。在EF Code First + Ria服務中驗證NotMapped屬性

此外,還編寫了一個代碼生成機制,爲每個這樣的「擴展」屬性生成「普通」c#屬性包裝。由於這種整個機制是更透明的,以開發

[NotMapped] 
    public string Version 
    { 
     get 
     { 
      return this.GetProperty(PropertyNameVersionConst) == null 
         ? null 
         : this.GetProperty(PropertyNameVersionConst).StringValue; 
     } 
     set 
     { 
      this.SetProperty(PropertyNameVersionConst, value);   
     } 
    } 

的問題是 - I可以使用例如([NotMapped])特性的驗證屬性?乍一看,我不明白爲什麼它不應該成爲可能。

[NotMapped] 
[Required] 
public string Version{...} 

我碰到其中一個驗證異常(在調用SaveChanges())被拋出與必需的特性這樣的屬性問題,即使該屬性已被設置爲一個非空值。

回答

1

好,添加虛擬改性劑的屬性後,開始正常工作

[NotMapped] 
public virtual string Version 
{ 
    get 
    { 
     return this.GetProperty(PropertyNameVersionConst) == null 
        ? null 
        : this.GetProperty(PropertyNameVersionConst).StringValue; 
    } 
    set 
    { 
     this.SetProperty(PropertyNameVersionConst, value);   
    } 
} 

任何人都可以細說了:)?

+0

我在我的模型中有一個值類型(結構)屬性,即使我將它標記爲'[NotMapped] EF仍然在保存之前驗證它的值。我也嘗試過'虛擬'修飾符,但仍然沒有機會 – sos00