title: Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details
body: <p>I'm Learning Webapi so I'm trying to build a simple Api connected to SQL server and I got this error when I add new Movie data</p>
  <p>Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
  ---&gt; Microsoft.Data.SqlClient.SqlException (0x80131904): The INSERT statement conflicted with the FOREIGN KEY constraint &quot;FK_Movies_SuperHeroes_HeroId&quot;. The conflict occurred in database &quot;SupersDb&quot;, table &quot;dbo.SuperHeroes&quot;, column 'HeroId'.</p>
  <p>I have two models :</p>
  <p>Superhero Model:</p>
  <pre><code>namespace SuperHeroesApi.Models
  {
      public class SuperHero
      {
          [Key]
          [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
          public int HeroId { get; set; }
  
          [Required]
          [MaxLength(100)]
          public string Name { get; set; } 
  
         
          [MaxLength(100)]
          public string FirstName { get; set; } 
  
          [MaxLength(100)]
          public string LastName { get; set; }
  
          [MaxLength(100)]
          public string City { get; set; } 
  
  
  
  
  
  
  
  
  
  
      }
  }
  </code></pre>
  <p>Movie Model :</p>
  <pre><code> namespace SuperHeroesApi.Models
  {
      public class Movie
      {
          [Key]
          [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
          public int MovieId { get; set; }
  
          [Required]
          [MaxLength(100)]
          public string Title { get; set; }
  
          public int Year { get; set; }
  
          public double Rate { get; set; }
  
          public byte [] Poster { get; set; }
  
          [ForeignKey(&quot;SuperHero&quot;)]
          public int HeroId { get; set; }
         
  
          //public string  SuuperHeroName { get; set; }
  
          public virtual SuperHero SuperHero { get; set; }
  
  
  
  
  
      }
  }
  </code></pre>
  <p>dto :</p>
  <pre><code>namespace SuperHeroesApi.Otds
  {
      public class MoviesDtos
      {
  
          public string Title { get; set; }
  
          public int Year { get; set; }
  
          public double Rate { get; set; }
  
          public IFormFile Poster { get; set; }
  
  
          [ForeignKey(&quot;SuperHero&quot;)]
          public int HeroId { get; set; }
  
  
  
      }
  }
  </code></pre>
  <p>MoviesController:</p>
  <pre><code>using SuperHeroesApi.Otds;
  
  namespace SuperHeroesApi.Controllers
  {
      [Route(&quot;api/[controller]&quot;)]
      [ApiController]
      public class MoviesController : ControllerBase
      {
  
          private readonly AppDbContext _dbContext;
  
          private new List&lt;string&gt; _allowedExtention = new List&lt;string&gt; { &quot;jbg&quot;, &quot;png&quot; };
          private long _maxAllowedPosterSize = 5242880;
  
          public MoviesController(AppDbContext dbContext)
          {
              _dbContext = dbContext;
          }
  
  
          [HttpGet]
          public async Task&lt;IActionResult&gt;GetAllAsync()
          {
              var movie = await _dbContext.Movies.ToListAsync();
  
              return Ok(movie);
  
          }
  
  
          [HttpPost]
          public async Task &lt;IActionResult&gt; CreateAsync([FromForm] MoviesDtos dto)
          {
              if (_allowedExtention.Contains(Path.GetExtension(dto.Poster.FileName).ToLower()))
                  return BadRequest();
  
              using var dataStream = new MemoryStream();
              await dto.Poster.CopyToAsync(dataStream);
  
  
              var movie = new Movie
              {
                  Title = dto.Title,
                  Year = dto.Year,
                  Rate = dto.Rate,
  
  
                  Poster = dataStream.ToArray(),
              };
  
                await _dbContext.AddAsync(movie);
                _dbContext.SaveChanges();
  
              return Ok(movie);
          }
  
  
  
  
      }
  }
  </code></pre>