2014-10-10 222 views
3

我试图更新我的MVC项目中的用户对象,但它似乎并没有保存我输入的新值。dbContext.SaveChanges()不保存和不输出错误

任何人都可以发现我做错了什么吗?

这是我进行保存的方法,我知道我通过运行我的Debug.WriteLines()一路下降到db.SaveChanges();

public bool editUser(string email, User innUser) 
{ 
    var db = new PastaContext(); 
    try 
    { 
     User editUser = db.Users.Find(email); 
     editUser.Firstname = innUser.Firstname; 
     Debug.WriteLine(innUser.Firstname); 
     Debug.WriteLine(editUser.Firstname); 
     editUser.Surname = innUser.Surname; 
     editUser.Address = innUser.Address; 
     editUser.Phonenr = innUser.Phonenr; 
     var newUser = new dbUser(); 
     byte[] passwordDb = createHash(innUser.Password); 
     newUser.Password = passwordDb; 
     newUser.Email = email; 


     if (editUser.Postcode != innUser.Postcode) 
     { 
      Debug.WriteLine("Inside in Postcode"); 
      // Postcode is altered. First check if the new postcode is in the tabel already 
      City existingCity = db.Cities.Find(innUser.Postcode); 
      if (existingCity == null) 
      { 
       Debug.WriteLine("Inside in not exsisting postcode"); 
       // poststedet eksisterer ikke, må legges inn 
       var newCity = new City() 
       { 
        Postcode = innUser.Postcode, 
        Cityname = innUser.city.Cityname 
       }; 
       db.Cities.Add(newCity); 
      } 
      else 
      { // city with the new postcode already exists, changing only postcode of user 
       editUser.Postcode = innUser.Postcode; 
       Debug.WriteLine("Inside in exsisting postcode"); 
      } 
     }; 
     Debug.WriteLine("Right before save"); 
     db.SaveChanges(); 
     Debug.WriteLine("Returning true"); 
     return true; 
    } 
    catch 
    { 
     Debug.WriteLine("returning false"); 
     return false; 
    } 
} 

输出捕捉异常的:错误的

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

那么你的调试输出是什么样子? – 2014-10-10 14:38:39

+0

当然!只要给我一下。 – mathletics 2014-10-10 14:46:14

+0

看起来你正在得到一个DbEntityValidationException。捕捉异常,它会告诉你你需要知道什么(EntityValidationErrors属性)。 – 2014-10-10 14:46:52

回答

1

当你创建你的新城市对象做到这一点:

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

感谢您的帮助! – mathletics 2014-10-10 16:34:01