wpf EntityFramework.dll 中发生类型为“System.Data.Entity...ModelValidationException”的未处理异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27736524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
An unhandled exception of type 'System.Data.Entity...ModelValidationException' occurred in EntityFramework.dll
提问by Kelk
So I'm in trouble with this error:
所以我遇到了这个错误:
An unhandled exception of type 'System.Data.Entity.ModelConfiguration.ModelValidationException' occurred in EntityFramework.dll.
EntityFramework.dll 中发生类型为“System.Data.Entity.ModelConfiguration.ModelValidationException”的未处理异常。
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.
所以这是 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; }
}
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! :)
希望有人能帮助我,祝你新年快乐!:)
回答by Bill Dinger
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.
这几乎可以肯定是因为您试图插入一个仅指定名称的 Kliendid,并且其他属性之一被标记为必需且不能为空。特别是在您的情况下,几乎可以肯定是 KliendiNumber 或 KliendiID,因为它们是整数并且不能为空,除非您使用 Keys 和身份列来自动递增它们。
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; }

