dbContext.SaveChanges()未保存且未输出错误

本文关键字:未输 输出 错误 保存 SaveChanges dbContext | 更新日期: 2023-09-27 18:26:08

我试图更新MVC项目中的User对象,但它似乎没有保存我输入的新值。

有人能认出我哪里做错了什么吗?

这是我保存的方法,我知道我通过运行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; }
}

dbContext.SaveChanges()未保存且未输出错误

当您创建新的城市对象时,您需要执行以下操作:

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