0

I am working on an API using Entity Framework and i added new models to the template model. when i do a migration, it is successful but when i run an update database from package manager console, it returns an error that says 'invalid column 'imagepath' in table asp.netusers'. please i do not know what else to do and i am sure the column exist in my table. below is my identity model

  public class ApplicationUser : IdentityUser
{
    //public string Username { get; set; } 

    //illustrates many bookings to one user
    public ICollection<Booking> Bookings { get; set; }
    //illustrates many feedacks to one user
    public ICollection<Feedback> feedbacks { get; set; }
    public string Alias { get; set; }
    //public string Address { get; set; }
    public DateTime DateCreated { get; set; }
    public string MobileNumber { get; set; }

    [NotMapped]
    public byte[] ImageArray { get; set; }
    public string ImagePath { get; set; }

below is my account controller

 [Required]
    [Display(Name = "Email")]
    [RegularExpression(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$", ErrorMessage = "Email in not valid")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    //custom Model
   //[Required]
   //[Display(Name ="Address")]
   //[StringLength(100,ErrorMessage ="Enter a Valid Address"),MinLength(10)]
   // public string Address { get; set; }
    public DateTime DateCreated { get; set; }

    [Required][Display(Name ="Mobile Number")]
    [RegularExpression("^[0-9]*$", ErrorMessage = "Invalid Phone Number")]
    [StringLength(11, ErrorMessage = "The {0} must be at least {2} characters long."), MinLength(6)]
    public string MobileNumber { get; set; }
    ////[Required]
    //[Display(Name = "User name")]
    ////[MaxLength(10, ErrorMessage = "The {0} must be at least {2} characters long."), MinLength(6)]
    //public string Username { get; set; }

    [StringLength(11, ErrorMessage = "The {0} must be at least {2} characters long."), MinLength(2)]
    public string Alias { get; set; }

    public string ImagePath { get; set; }
    public byte[] ImageArray { get; set; }

lastly below is my AccountController class

  public async Task<IHttpActionResult> Register( RegisterBindingModel model)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        else
        {
            var stream = new MemoryStream(model.ImageArray);
            var guid = Guid.NewGuid().ToString();
            var file = string.Format("{0}.jpg", guid);
            var folder = "~\\Content\\UserData";
            var fullpath = string.Format("{0}\\{1}", folder, file);
            var response = FileHelper.UploadPhoto(stream, folder, file);

            var user = new ApplicationUser()
            {
                UserName = model.Alias,
                Email = model.Email,               
                DateCreated = model.DateCreated,
                MobileNumber = model.MobileNumber,
                Alias = model.Alias,
                ImagePath = fullpath
            };


            IdentityResult result = await UserManager.CreateAsync(user, model.Password);
            if (!result.Succeeded)
            {
                return GetErrorResult(result);
            }

            return Ok(user);

The error comes up with 'invalid column "Imagepath" on asp.net-user table' and i am sure i have the column because i have saved in the column before.

Classyk
  • 55
  • 1
  • 10
  • So to confirm the error is when trying to run update-database? Was the `ImagePath` field on the database before? What does the migration file say is changing vs what is in the migration snapshot around the `ImagePath` field. If it's happening on update-database then it's probably got nothing to do with your controller or `RegisterBindingModel`. Need a little more relevant information – Topher Mar 03 '19 at 06:14
  • Thanks Topher, the ImagePath field is in the database before because I have saved in it. When I tried to send data from postman, it just says the column is invalid. And then I try to remove the column and run a migration and then update-database command and it said it can't drop the column because it does not exist. The migration snapshot around the ImagePath around is that it dropped the column and the error only comes up during update-database command. I hope it's enough information, please kindly let me know if you need to know any other thing – Classyk Mar 03 '19 at 09:38
  • can you include that information in your question, like a screenshot of the package manager console with the error, maybe a screenshot of your database table columns – Topher Mar 03 '19 at 10:03
  • otherwise maybe you can try going down the route this guy followed: https://stackoverflow.com/a/32778220/3763021 – Topher Mar 03 '19 at 10:05

0 Answers0