2016-05-12 58 views
0

私は以下の2つのドメインモデルを持っています。1つまたは複数のエンティティの検証に失敗しました。詳細については、EntityValidationErrorsプロパティを参照してください。

[Table("tblActual_AgencyProfile")] 
public class AgencyModel 
{ 
    public AgencyModel() 
    { 
     CategoryItemList = new List<CategoryModel>(); 
     user = new UserProfile(); 
    } 

    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int ID { get; set; } 
    public string UniqueURL { get; set; } 
    public string Name { get; set; } 
    public string Title { get; set; } 
    public string Address { get; set; } 
    public string Phone { get; set; } 
    public string Fax { get; set; } 
    public string Email { get; set; } 
    public string ABN { get; set; } 
    public string CAN { get; set; } 
    public string PhotoURL { get; set; } 
    public string Summary { get; set; } 
    public string Specialities { get; set; } 
    public string LocationsServices { get; set; } 
    public DateTime? ImportedDateTime { get; set; } 
    public int? UserID { get; set; } 

    [ForeignKey("UserID")] 
    public UserProfile user { get; set; } 

    public ICollection<CategoryModel> CategoryItemList { get; set; } 
} 


[Table("UserProfile")] 
public class UserProfile 
{ 
    public UserProfile() 
    { 
     Actual_Category_List = new List<CategoryViewModel>(); 
    } 

    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int UserId { get; set; } 
    public string UserName { get; set; } 
    [Required] 
    public string UserType { get; set; } 
    [RequiredIfAttribute("UserType", "Agency")] 
    public string AgencyName { get; set; } 
    public string Title { get; set; } 
    [Required] 
    public string FirstName { get; set; } 
    [Required] 
    public string LastName { get; set; } 
    [Required] 
    public string Address { get; set; } 
    [Required] 
    public string Suburb { get; set; } 
    [Required] 
    public string State { get; set; } 
    [Required] 
    public string PostCode { get; set; } 
    [Required] 
    public string Phone { get; set; } 
    public string Fax { get; set; } 
    public string Mobile { get; set; } 
    [EmailAddress] 
    [Required] 
    public string EmailAddress { get; set; } 
    public ICollection<CategoryViewModel> Actual_Category_List { get; set; } 
} 

は、その後、私はあなたが見ることができるように、私はUserProfile(:FirstNameLastName例)内のすべてのフィールドを更新しない

public void EditDetails(AgencyViewModel model) 
{ 
    try 
    { 
     using (WebScrapperDBContext contex = new WebScrapperDBContext()) 
     { 
      using (TransactionScope scope = new TransactionScope()) 
      { 
       AgencyModel agency = (from tb in contex.agencies where tb.ID == model.ID select tb).SingleOrDefault(); 
       agency.Address = model.Address; 
       agency.Email = model.Email; 
       agency.Fax = model.Fax; 
       agency.LocationsServices = model.LocationsServices; 
       agency.Name = model.Name; 
       agency.Phone = model.Phone; 
       if (!string.IsNullOrEmpty(agency.PhotoURL)) 
        agency.PhotoURL = model.PhotoURL; 
       agency.Specialities = model.Specialities; 
       agency.Summary = model.Summary; 

       agency.user.Address = model.Address; 
       agency.user.AgencyName = model.Name; 
       agency.user.EmailAddress = model.Email; 
       agency.user.Fax = model.Fax; 
       agency.user.Mobile = model.Mobile; 
       agency.user.Phone = model.Phone; 
       agency.user.PostCode = model.PostCode; 
       agency.user.State = model.State; 
       agency.user.Suburb = model.Suburb; 

       contex.SaveChanges(); 

       scope.Complete(); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     throw ex; 
    } 
} 

私のリポジトリクラスのUpdateメソッドを持っています。 EditDetails()メソッドを実行すると、例外がスローされます

1つ以上のエンティティの検証に失敗しました。詳細については、 「EntityValidationErrors」プロパティを参照してください。

これは、ドメインモデルでいくつかの必須フィールドをスキップしている可能性があるためです。どうすればこの問題を解決できますか?

+0

詳細については、「EntityValidationErrors」プロパティを参照してください。エラーについて教えてください。 –

+0

@StephenMuecke私はEntityValidationErrorsをチェックしました。正確には、必要なフィールドの検証エラーが発生していると思った。 – chamara

+1

クエリに '.Include(x => x.user)'を持つ必要があるでしょう。そうすれば、必要なものだけをpdateできるように、 'user'プロパティに既存の値が設定されます。 –

答えて

1

このエラーは、userというプロパティの値が、クエリが返すモデルに含まれていないことを示しています。クエリに.Include(x => x.user)を追加することで、これらのデータを入力することができます。その後、必要なプロパティを更新するだけで済みます。

0

この設定を使用して、更新時の検証を無視できます。あなたの場合は、すべてではなくいくつかのプロパティを更新するだけです。

context.Configuration.ValidateOnSaveEnabled = false; 
context.SaveChanges(); 
関連する問題

 関連する問題