2016-04-29 18 views
0

テーブル内の行を更新しようとしていますが、このアクションによってDbEntityValidationExceptionが生成されます。エンティティ・フレームワークを使用してテーブルの行を更新しようとしたときにDbEntityValidationExceptionが発生する

 public void UpdateLastReviewCreationDate(string idBusiness, DateTime LastReviewCreationDate,string urlSite) { 
 

 
      DAL.Business bu; 
 
      try { 
 
       using (DBContext tpContext = new DBContext()) 
 
       { 
 
        bu = tpContext.business.Find(idBusiness, urlSite); 
 
        if (bu != null) 
 
        { 
 
         bu.LastReviewCreationDate = LastReviewCreationDate; 
 
         tpContext.SaveChanges(); 
 
        } 
 
       } 
 
      } 
 
      catch (DbEntityValidationException e) 
 
      { 
 
       foreach (var eve in e.EntityValidationErrors) 
 
       { 
 
        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", 
 
         eve.Entry.Entity.GetType().Name, eve.Entry.State); 
 
        foreach (var ve in eve.ValidationErrors) 
 
        { 
 
         Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", 
 
          ve.PropertyName, ve.ErrorMessage); 
 
        } 
 
       } 
 
       throw; 
 
      } 
 
     }

だから、これは私がデバッガで得たものである: enter image description here

このテーブルは読み取り専用と編集を許可されていない手段だこと。しかし、私はこのテーブルをRead-onlyで設定したことはありません。本当に変です。これはこの表のコードです。

public class Business 
 
    { 
 
     public Business() 
 
     { 
 
      LastReviewCreationDate = new DateTime(); 
 
     } 
 

 
     [Key, Column(Order = 0)] 
 
     public string Id { get; set; } 
 

 
     [Key, Column(Order = 1)] 
 
     [ForeignKey("site")] 
 
     public string SiteId { get; set; } 
 

 
     public string UrlBusiness { get; set; } 
 
     public DateTime LastReviewCreationDate { get; set; } 
 

 
     [Required] 
 
     public virtual Site site { get;set;} 
 
     public virtual BusinessUser businessUser{get;set;} 
 

 
    }

たぶん誰もがアイデアを持っていますか?そしてfalseにパラメータIsReadOnlyをリセットすることは可能ですか?

+0

IsReadOnlyは、設定可能なエントリのフラグです。しかし、何か間違っているはずです。あなたのコンテキストビジネスの資産がDbSet であることを確かめますか?どのように構成されていますか(流暢なAPI?) – DevilSuichiro

+0

私は 'public DbSet business {get;セット; } '私のコンテキスト上で。 – Stephane

+0

このdbsetに関するこのdbset(コンテキストなど)はどのように設定されていますか?私はあなたのエントリ(-set)でreadonlyをオフにすることができますが、DbContextの標準的な動作は読み込み専用ではありません。 – DevilSuichiro

答えて

1

最後に、この問題を自分で修正しました。 これは私がcatched例外です:

Property: location, Error: The site field is required. 

を、私は、サイトへのフィールドの必要なプロパティを設定しているため、そのたび私は、この表を更新したいサイトの値を変更する必要がない場合であっても、それは常に必要クライアントコードによって値が与えられていること。

public class Business 
 
    { 
 
     public Business() 
 
     { 
 
      LastReviewCreationDate = new DateTime(); 
 
     } 
 

 
     [Key, Column(Order = 0)] 
 
     public string Id { get; set; } 
 

 
     [Key, Column(Order = 1)] 
 
     [ForeignKey("site")] 
 
     public string SiteId { get; set; } 
 

 
     public string UrlBusiness { get; set; } 
 
     public DateTime LastReviewCreationDate { get; set; } 
 

 
     [Required] 
 
     public virtual Site site { get;set;} 
 
     public virtual BusinessUser businessUser{get;set;} 
 

 
    }

だから私は単にtpContext.SaveChanges();

 public void UpdateLastReviewCreationDate(string idBusiness, DateTime LastReviewCreationDate,string urlSite) { 
 

 
      DAL.Business bu; 
 
      try { 
 
       using (DBContext tpContext = new DBContext()) 
 
       { 
 
        bu = tpContext.business.Find(idBusiness, urlSite); 
 
        DAL.Site siteE = bu.site; 
 
        if (bu != null) 
 
        { 
 
         bu.LastReviewCreationDate = LastReviewCreationDate; 
 
         bu.site = siteE; 
 
         tpContext.SaveChanges(); 
 
        } 
 
       } 
 
      } 
 
      catch (DbEntityValidationException e) 
 
      { 
 
       foreach (var eve in e.EntityValidationErrors) 
 
       { 
 
        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", 
 
         eve.Entry.Entity.GetType().Name, eve.Entry.State); 
 
        foreach (var ve in eve.ValidationErrors) 
 
        { 
 
         Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", 
 
          ve.PropertyName, ve.ErrorMessage); 
 
        } 
 
       } 
 
       throw; 
 
      } 
 
     }

bu.site = siteE;を追加することによってこの問題を解決する、これが役立つことを願っています。

0

データベースにデータを書き込むことができないような状況が発生しています。 実際には、デバッグ中にVisual Studioでその配列にドリルするとエラーが表示されるはずです。しかし、例外をキャッチして、いくつかのロギングストアまたはコンソールにエラーを書き出すこともできます。

関連する問題