Kelk
Kelk

Reputation: 63

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

So I'm in trouble with this error:

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);
        }

So this was a part of a main script in wpf and i wrote a comment where visual studio compiler shows error for me.

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

and last class what is used to make database and propably there is a error

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; }

}

Hope that somebody can help me out so cheers and happy new year! :)

Upvotes: 0

Views: 5561

Answers (1)

Bill Dinger
Bill Dinger

Reputation: 179

This is almost certainly because you are attempting to insert a Kliendid with only a name specified and one of the other properties is marked as Required and cannot be null. Specifically in your case it's almost certainly the KliendiNumber or KliendiID as those are integers and can't be null, unless you were using Keys and identity columns to auto increment them.

You have a couple options. One, you could actually look at the exception and see which property and error it is by trapping the exception and looking at the validation errors property.

 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);
          }
        }
}

You can also change the model to allow nullable integers on those 2 fields

public int? KliendiID { get; set; }
public int? KliendiNumber { get; set; }

Upvotes: 2

Related Questions