1

I am using a SQL database called as MusicChannel which will hold the new added songs with artists. When I try to add something, it gives me a error saying:

SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Songs__ArtistID__36B12243". The conflict occurred in database "MusicChannel", table "dbo.Artists", column 'ArtistID'. The statement has been terminated.

I created the table in order of Artists, MusicTypes and Songs. Songs have 2 FK as ArtistID and MusicTypeID. Artists' PK is ArtistID. MusicTypes' PK is MusicTypeID. Is this happening because the names are the same?

Here is the Model:

public IActionResult Insert(NewSongVm formContent)
        {
            if (formContent.MusicTypeID == -1)
            {
                //
            }
            MusicChannelContext ctx = new MusicChannelContext();
            Song song = new Song();
            Artist artist = new Artist();
            song.SongID = formContent.SongID;
            song.SongName = formContent.SongName;
            song.SongLength = formContent.SongLength;
            song.SongLink = formContent.SongLink;
            song.MusicTypeID = formContent.MusicTypeID;
            song.ArtistID = formContent.ArtistID;
            artist.ArtistID = formContent.ArtistID;
            artist.ArtistName = formContent.ArtistName;
            ctx.Artists.Add(artist);
            ctx.Songs.Add(song);
            ctx.SaveChanges();
            return View();
        }
 
//context:

 public class MusicChannelContext:DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("server=.;database=MusicChannel;trusted_connection=true;");  
        }
        public DbSet<Song> Songs { get; set; }
        public DbSet<Artist> Artists { get; set; }
        public DbSet<MusicType> MusicTypes { get; set; }
    }

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • 1
    What about the error don't you understand? It's telling you the problem here. if you explain what confuses you, we can try to elaborate on the areas that confuse you. – Thom A Dec 01 '21 at 11:50
  • 1
    Does this answer your question? [INSERT statement conflicted with the FOREIGN KEY constraint - SQL Server](https://stackoverflow.com/questions/2965837/insert-statement-conflicted-with-the-foreign-key-constraint-sql-server) – derpirscher Dec 01 '21 at 11:50
  • Please check out the [thread](https://stackoverflow.com/questions/2965837/insert-statement-conflicted-with-the-foreign-key-constraint-sql-server).This might resolve your issue. – Ishtiaq Dec 01 '21 at 11:53

1 Answers1

0

You need to save changes in Artist table, Add command does not create a record in the database,

Another solution in drop FK constraint from table in database, then you can create records in any orders you want.

Check flowing sample:

         MusicChannelContext ctx = new MusicChannelContext();
        Artist artist = new Artist();
        artist.ArtistID = formContent.ArtistID;
        artist.ArtistName = formContent.ArtistName;
        ctx.Artists.Add(artist);
        ctx.SaveChanges();

        Song song = new Song();
        song.SongID = formContent.SongID;
        song.SongName = formContent.SongName;
        song.SongLength = formContent.SongLength;
        song.SongLink = formContent.SongLink;
        song.MusicTypeID = formContent.MusicTypeID;
        song.ArtistID = formContent.ArtistID;

        ctx.Songs.Add(song);
        ctx.SaveChanges();
Aqil
  • 360
  • 2
  • 16