2015-01-02 93 views
0

所以我就麻煩了這個錯誤:類型的未處理的異常「System.Data.Entity的...... ModelValidationException」發生在EntityFramework.dll

An unhandled exception of type 'System.Data.Entity.ModelConfiguration.ModelValidationException' occurred in EntityFramework.dll.

private void button_Click(object sender, RoutedEventArgs e) 
{ 
     MessageBox.Show("Are you sure?"); 

     var db = new AndmebaasContext(); 
     Console.WriteLine("vajtuasid comboboxi"); 
     string name = "Peeter"; 
     int number = 5081976; 
     long isikukood = 39502266016; 
     bool Kliendikaart = true; 

     //DateTime TulebJargi = new DateTime(02, 01, 2015); 
     // DateTime ToobTagasi = new DateTime(23, 01, 2015); 

     string lumelauad = "Flow 150"; 
     string MaeSuusad = "Rossignol 170"; 

     var kliendinimi = new Kliendid { KliendiNimi = name}; 
     db.Kontakt.Add(kliendinimi);// ERROR Seems to be HERE!!! 
     db.SaveChanges(); 

     var query = from b in db.Kontakt 
        orderby b.KliendiNimi 
        select b; 


     foreach (var item in query) 
     { 
      Console.WriteLine(item.KliendiNimi); 
     } 

因此,這是一部分wpf中的一個主要腳本,我在visual studio編譯器爲我顯示錯誤的地方寫了一條評論。

public class AndmebaasContext : DbContext 
{ 
    public DbSet<Kliendid> Kontakt { get; set; } 
    public DbSet<KliendiRendiAndmed> KliendiRendiAndmed { get; set; } 
    public DbSet<KliendiRenditudVarustus> KliendiRenditudVarustus { get; set; } 
} 

,哪些是用來做數據庫和最後一堂課propably有一個錯誤

public class Kliendid 
{ 
    public int KliendiID { get; set; } 
    public string KliendiNimi { get; set; } 
    public int KliendiNumber { get; set; } 
    public long KliendiIsikukood { get; set; } 
    public bool KliendiKliendikaart { get; set; } 
    public virtual List<KliendiRendiAndmed> Kliendirendiandmed { get; set; } 

} 

public class KliendiRendiAndmed 
{ 
    public int KliendiRendiAndmeteID { get; set; } 
    public DateTime TulebJärgi { get; set; } 
    public DateTime ToobTagasi { get; set; } 

    public virtual List<KliendiRenditudVarustus> Kliendirenditudvarustus {get; set;} 
} 
public class KliendiRenditudVarustus 
{ 

    public int KliendiRenditudVarustuseID { get; set; } 
    public string LumeLaud { get; set; } 
    public string LumeLauaSaapad { get; set; } 
    public string MaeSuusk { get; set; } 
    public string MaeSuusaSaapad { get; set; } 
    public string SuusaKepid { get; set; } 

    public virtual Kliendid Kliendid { get; set; } 

} 

希望有人可以幫助我這樣的歡呼和新年快樂! :)

回答

2

這幾乎肯定是因爲您試圖插入Kliendid僅指定一個名稱,其他屬性之一標記爲必需,不能爲空。特別是在你的情況下,它幾乎肯定是KliendiNumber或KliendiID,因爲它們是整數,不能爲空,除非你使用Keys和標識列來自動增加它們。

你有幾個選項。一個,你可以看看異常,並通過捕獲異常並查看驗證錯誤屬性來查看它是哪個屬性和錯誤。

Catch (DbEntityValidationException ex) 
{ 
    foreach (var validationError in ex.EntityValidationErrors) { 
     foreach (var errorDetail in validationError.ValidationErrors) 
     { 
      Console.WriteLine("Property: {0} Error: {1}", 
      errorDetail.PropertyName, errorDetail.ErrorMessage); 
      } 
     } 
} 

你也可以改變模型,讓那些2場可空整數

public int? KliendiID { get; set; } 
public int? KliendiNumber { get; set; } 
相關問題