0
votes

I am having a problem please I need your help: Actually after adding ASPIdentity to my app i did the migration which went well but when i want to update with update-database command i get this error in console manager:

Cannot insert the value NULL into column 'ZipCode', table 'LibrairyBookASPCore.dbo.Orders'; column does not allow nulls. UPDATE fails. The statement has been terminated.

This is code of my Order Class:

enter code here
public class Order
{
    [BindNever]
    [Key]
    public int OrderId { get; set; }
    public List <OrderDetail> OrderDetails { get; set; }

    [Required(ErrorMessage ="Please enter your first name")]
    [Display(Name ="First name")]
    [StringLength(50)]
    public String FirstName { get; set; }

    [Required(ErrorMessage = "Please enter your last name")]
    [Display(Name = "Last name")]
    [StringLength(50)]
    public String  LastName { get; set; }

    [Required(ErrorMessage = "Please enter your address")]
    [Display(Name = "Address name")]
    [StringLength(200)]
    public String AdressLine1 { get; set; }

    public String AdressLine2 { get; set; }

    [Required(ErrorMessage = "Please enter your zip code")]
    [Display(Name = "Zip code")]
    [StringLength(10,MinimumLength = 4)]
    public String ZipCode { get; set; }

    [Required(ErrorMessage = "Please enter your city")]
    [StringLength(50)]
    public String City { get; set; }

    public String State { get; set; }

    [Required(ErrorMessage = "Please enter your country")]
    [StringLength(50)]
    public String Country { get; set; }

    [Required(ErrorMessage = "Please enter your phone number")]
    [StringLength(25)]
    [DataType(DataType.PhoneNumber)]
    [Display(Name ="Phone number")]
    public String PhoneNumber { get; set; }

    [Required]
    [StringLength(50)]
    [DataType(DataType.EmailAddress)]
    [RegularExpression (@"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z",
    ErrorMessage = "The email address is not entered in a correct format")]
    //(@"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|""(?:[\x01-\x08\x0b\x0c\x0e-\x1f]
    public String Email { get; set; }

    [BindNever]
    public  decimal OrderTotal { get; set; }

    [BindNever]
    public DateTime OrderPlaced { get; set; }

}

}

this is my order table in database enter image description here

Thank you

3
And this has what to do with the C language ? - WhozCraig
sorry. it's c# language - Vaujany
apparently, you already have data in your DB. and you're adding a new column that must not be NULL (since it's required). so where should EF get the values for those datasets to fill the column from? either make the column nullable or add a default value. - Franz Gleichmann
error is very clear, data you are updating is missing required fields. Maybe validate your data before interacting with DB - Nonik
I made the Zipcode column nullable, but it didn't always work - Vaujany

3 Answers

1
votes

You have the required attribute on the zip code. [Required(ErrorMessage = "Please enter your zip code")]

This means you have to have this data when trying to add to the database. Looking at the image you uploaded with your question, it looks like no data is being passed to your save method.

I would check your controller to make sure that the update method you are calling is actually receiving any data, and trying to update any data it receives correctly.

0
votes

You have the Required attribute on the ZipCode property in your class

And in your database you are allowing null

-2
votes

Here is in fact I solved my problem: 1- I first commented on the addition of the user authentication service in the "ConfigureServices" method

2- I also commented on the use of the service in the "Configure" method 3-I then remove the migrations and I start the procedure again. Thank you for your contributions.enter image description here