Thursday, February 7, 2019

EF Core not generating ID

I have a controller to post an image and thanks to help from the subreddit I was able to get saving the image working properly. Now I want to save the file name and an associated ID to the DB as well as have EF core create the ID for the entry. But it is giving me a 'cannot insert null' error, which is true, it cannot, but I thought EF core was suppose to generate that ID? Here is my entity -

 public class FurnitureImage { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int FurnitureImagesId { get; set; } [Required] public int FurnitureId { get; set; } [Required] public string PictureInfo { get; set; } } 

And here is my controller -

[Route("upload")] [HttpPost] [Consumes("multipart/form-data")] public async Task<IActionResult> UploadFile(IFormFile image) { var furnitureImage = new FurnitureImage(); if (ModelState.IsValid) { if (image != null && image.Length > 0) { var fileName = Path.GetFileName(image.FileName); var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\NewFolder", fileName); using (var fileSteam = new FileStream(filePath, FileMode.Create)) { await image.CopyToAsync(fileSteam); } //Hard coding the furnitureId for testing purposes. furnitureImage = new FurnitureImage() { PictureInfo = fileName, FurnitureId = 9 }; } } _furnitureInfoRepository.AddNewFurnitureImage(furnitureImage); return Ok(furnitureImage); } 

But when I make a POST from PostMan it throws me an error saying I cannot insert null in the FurnitureImagesId column. What am I doing wrong?

EF Core not generating ID Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team