I created a database using code-first method. Now when I try to add a showing to my Cinema database I get an DbupdateException. And it says
Violation of PRIMARY KEY constraint 'PK_dbo.Movies'. Cannot insert duplicate key in object 'dbo.Movies'. The duplicate key value is (The Room)
I don't understand why. I'm not adding a new movie with the name The Room, I'm adding a showing with a new key.
namespace Cinema3Project.Tables
{
class Showing
{
[Key]
public int Number { get; set; }
public DateTime Date { get; set; }
public TimeSpan Time { get; set; }
public virtual Movie Movie { get; set; }
public virtual Screen Screen { get; set; }
}
}
class Movie
{
[Key]
public string Name { get; set; }
public string Director { get; set; }
public string Genre { get; set; }
public string Language { get; set; }
public int LengthMin { get; set; }
}
Inserting method looks like this:
using (var db = new CinemaContext())
{
db.Showings.Add(showing);
db.SaveChanges();
}