C# dbContext.SaveChanges()不保存也不输出错误
我试图在我的MVC项目中更新一个用户对象,但它似乎没有保存我输入的新值 有人能看出我做错了什么吗 这就是我进行保存的方法,我知道我可以一直保存到C# dbContext.SaveChanges()不保存也不输出错误,c#,asp.net-mvc,entity-framework,visual-studio,C#,Asp.net Mvc,Entity Framework,Visual Studio,我试图在我的MVC项目中更新一个用户对象,但它似乎没有保存我输入的新值 有人能看出我做错了什么吗 这就是我进行保存的方法,我知道我可以一直保存到db.SaveChanges()
db.SaveChanges()通过运行myDebug.WriteLines()
捕获异常的输出:
System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at Oblig1.DBUser.editUser(String email, User innUser) in c:\Users\XXX\XXX\XXX\XXX\DBUser.cs:line 55
错误的输出:
A first chance exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in EntityFramework.dll
Entity of type "City" in state "Added" has the following validation errors:
- Property: "Postcode", Error: "Postcode must be added"
Entity of type "User_546F9926F8DC53E2EF66BA48BE431DF1B26DEBEFA54B0597E60AEB1839DD022C" in state "Modified" has the following validation errors:
- Property: "city", Error: "The city field is required."
我使用的模型有三个表:
public class User
{
[Required(ErrorMessage = "Firstname must be added")]
public string Firstname { get; set; }
[Required(ErrorMessage = "Surname must be added")]
public string Surname { get; set; }
[Required(ErrorMessage = "Address must be added")]
public string Address { get; set; }
public string Postcode { get; set; }
[Key]
[Required(ErrorMessage = "E-mail must be added")]
[DataType(DataType.EmailAddress)]
[EmailAddress]
public string Email { get; set; }
[Required(ErrorMessage = "Phonenr must be added")]
public string Phonenr { get; set; }
[Required(ErrorMessage = "Password must be added")]
public string Password { get; set; }
[Required]
public virtual City city { get; set; }
public virtual List<Order> Orders { get; set; }
}
public class dbUser
{
[Key]
public string Email { get; set; }
public byte[] Password { get; set; }
}
public class City
{
[Key]
[Required(ErrorMessage = "Postcode must be added")]
public string Postcode { get; set; }
[Required(ErrorMessage = "City must be added")]
public string Cityname { get; set; }
}
公共类用户
{
[必需(ErrorMessage=“必须添加Firstname”)]
公共字符串名{get;set;}
[必需(ErrorMessage=“必须添加姓氏”)]
公共字符串姓氏{get;set;}
[必需(ErrorMessage=“必须添加地址”)]
公共字符串地址{get;set;}
公共字符串邮政编码{get;set;}
[关键]
[必需(ErrorMessage=“必须添加电子邮件”)]
[数据类型(数据类型.电子邮件地址)]
[电邮地址]
公共字符串电子邮件{get;set;}
[必需(ErrorMessage=“必须添加Phonenr”)]
公共字符串Phonenr{get;set;}
[必需(ErrorMessage=“必须添加密码”)]
公共字符串密码{get;set;}
[必需]
公共虚拟城市城市{get;set;}
公共虚拟列表顺序{get;set;}
}
公共类dbUser
{
[关键]
公共字符串电子邮件{get;set;}
公共字节[]密码{get;set;}
}
公营城市
{
[关键]
[必需(ErrorMessage=“必须添加邮政编码”)]
公共字符串邮政编码{get;set;}
[必需(ErrorMessage=“必须添加城市”)]
公共字符串Cityname{get;set;}
}
创建新城市对象时,请执行以下操作:
var newCity = new City()
{
Postcode = innUser.Postcode,
Cityname = innUser.city.Cityname
};
相反,您需要使用City.postcode
属性设置邮政编码,如下所示:
var newCity = new City()
{
Postcode = innUser.city.Postcode,
Cityname = innUser.city.Cityname
};
调试输出是什么样子的?当然!给我一点时间。看起来你得到了一个DbEntityValidationException。捕获异常,它将告诉您需要知道的内容(EntityValidationErrors属性)。您确定连接已打开吗@adamNature不够,您需要阅读EntityValidationErrors
集合。
var newCity = new City()
{
Postcode = innUser.city.Postcode,
Cityname = innUser.city.Cityname
};