DbContext.SaveChanges () does not save and does not throw an error

I am trying to update a User object in my MVC project, but it does not store the new values ​​that I entered.

Can anyone identify where I am doing something wrong?

This is the method in which I do save, I know that before the end db.SaveChanges();

came to db.SaveChanges();

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

      

Exception trap exit:

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

      

Error output:

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."

      

The model I'm using with three tables:

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

      

+3


source to share


1 answer


When you create your new city object, you do this:

var newCity = new City()
{
    Postcode = innUser.Postcode,
    Cityname = innUser.city.Cityname
};

      



Instead, you need to set the zip code using a property City.Postcode

like this:

var newCity = new City()
{
    Postcode = innUser.city.Postcode,
    Cityname = innUser.city.Cityname
};

      

+1


source







All Articles