dbContext.SaveChanges()不保存而不输出错误

时间:2014-10-10 14:30:54

标签: c# asp.net-mvc entity-framework visual-studio

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

有人能发现我做错了吗?

这是我执行保存的方法,我知道通过运行db.SaveChanges();,我可以一直到Debug.WriteLines()

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 个答案:

答案 0 :(得分: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
};
相关问题