2014-01-15 42 views
1

我有兩個表,User和UserPhoto,這些表用一對多關係表示。我想有一個UserRepository類,它具有AddUser/UpdateUser/DeleteUser方法,還有AddUserPhoto和DeleteUserPhoto,因爲我想實現一些邏輯,我想實現添加和刪除存儲庫層中包含的用戶照片。EF倉庫驗證失敗原因不明

在AddUserPhoto方法中,我從集合中檢索User對象,添加照片,將set修改爲true並返回驗證結果。如下

public bool AddUserPhoto(DbUserPhoto photo, object userId) 
     { 
      try 
      { 
       var dbItem = Context.Users.Find(userId); 
       if (dbItem == null) 
        throw new Exception(string.Format("User id {0} not found" 
                , userId)); 

       // Some logic around ordering of photos and only having 
       //one primary photo 
       if (photo.IsPrimary) 
       { 
        foreach (var p in dbItem.Photos) 
        { 
         p.IsPrimary = false; 
         p.DisplayOrder++; 
        } 

        photo.DisplayOrder = 1; 
       } 
       else 
       { 
        var maxDisplayOrder = dbItem.Photos 
               .Max(p => p.DisplayOrder); 
        photo.DisplayOrder = maxDisplayOrder + 1; 
       } 

       dbItem.Photos.Add(photo); 
       Context.Users.Attach(dbItem); 
       Context.Entry(dbItem).State = EntityState.Modified; 
       return Context.Entry(dbItem).GetValidationResult().IsValid; 
      } 
      catch (Exception ex) 
      { 
       if (ex.InnerException != null) 
        throw new DataRepositoryException(ex.InnerException.Message, 
           "UserDataRepository.InsertItem", 
           ex.InnerException); 

       throw new DataRepositoryException(ex.Message, 
          "UserDataRepository.InsertItem", 
          ex); 
      } 
     } 

我遇到的問題是驗證返回用戶實體的IsValid = False。我已經檢查過,並且一旦從數據庫中檢索到,從Find()操作返回的用戶的IsValid = False,表示「位置字段是必需的」。

位置是User的屬性,它是強制性的,並且引用另一個表,稱爲GeographicalLocation,因此用戶的Location屬性應該是有效的GeographicalLocation對象。

我已經檢查使用手錶和用戶(當返回)肯定有一個有效的位置分配給位置屬性,所以我很困惑,爲什麼它是失敗的驗證。

有什麼想法?

編輯:下面

[Table("User")] 
    public class DbUser : IEntityComparable<DbUser> 
    { 
     [Key] 
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public int UserId { get; set; } 

     [Required] 
     [MaxLength(80)] 
     public string FirstName { get; set; } 

     [Required] 
     [MaxLength(80)] 
     public string LastName { get; set; } 

     [Required] 
     [MaxLength(80)] 
     public string EmailAddress { get; set; } 

     [Required] 
     public DateTime DateOfBirth { get; set; } 

     [Required] 
     [MaxLength(1)] 
     public string Gender { get; set; } 

     [Required] 
     public virtual DbGeographicalArea Location { get; set; } 

} 
+0

,你能否告訴了'User'類? – Colin

+0

如果dbItem不包含GeographicalLocation對象,請將其包含並重試 –

+0

添加用戶類。多一點的調試似乎表明,如果我通過代碼進行調試並打開用戶變量並顯示其中包含的Location對象,則可以運行代碼並運行。我猜這是與延遲加載有關嗎?如何在上面的User類中指定不延遲加載GeographicalArea對象? – NZJames

回答

1

用戶I類將從該Location屬性中刪除[Required]數據標註,並添加一個外鍵:

//You don't need a [Required] annotation on an int because it isn't nullable 
public int LocationID {get; set;} 

public virtual DbGeographicalArea Location { get; set; } 

LocationIDUsers表,以便它將填充而不加入DbGeographicalAreas表 - 這是如果您使用Include

如果您添加外鍵,實體框架更容易使用。

參考文獻:

Why does Entity Framework Reinsert Existing Objects into My Database?

Making Do with Absent Foreign Keys

+0

好的提示,非常感謝! – NZJames

相關問題