[英]How to solve System.Data.Entity.Infrastructure.DbUpdateException
The INSERT
statement conflicted with the FOREIGN KEY constraint FK_dbo.CinemaShowTime_dbo.Cinema_cinemaId
. INSERT
语句与FOREIGN KEY约束FK_dbo.CinemaShowTime_dbo.Cinema_cinemaId
冲突。 The conflict occurred in database " CinemaModule
", table " dbo.Cinema
";数据库“
CinemaModule
”,表“ dbo.Cinema
”发生冲突;
I am successfully saving the result of Cinema in DB but when I try to save movie or CreateCinemasShowTime
it give me error.我成功地将 Cinema 的结果保存在 DB 中,但是当我尝试保存电影或
CreateCinemasShowTime
它给了我错误。
I am trying to render multiple models into single view but DBUPdateException
occurs.我试图将多个模型渲染到单个视图中,但发生
DBUPdateException
。
My Controller code我的控制器代码
var cinema = new Cinema();
cinema.Halls = viewModel.Halls;
cinema.ContactNumber = viewModel.ContactNumber;
cinema.TicketFee = viewModel.TicketFee;
var movie = new Movies();
movie.MovieName = viewModel.MovieName;
movie.MovieBio = viewModel.MovieBio;
movie.MoviePosterUrl = viewModel.MoviePosterUrl;
var cinemaShowtime = new CinemaShowTime();
cinemaShowtime.ShowDay = viewModel.ShowDay;
cinemaShowtime.ShowTime = viewModel.ShowTime;
db.CinemaShowTimes.Add(cinemaShowtime);
db.SaveChanges();
My Model Code我的型号代码
public int Id { get; set; }
public string MovieName { get; set; }//
public string MoviePosterUrl { get; set; }//
public string MovieBio { get; set; }
//collection of CinemaShowTimes for single movie
public virtual ICollection<CinemaShowTime> CinemaShowTimes { get; set; }
//single industry for each Movie
[ForeignKey("IndustriesInstance")]
public int? IndustryId { get; set; }
//reference navigation property
public virtual Industries IndustriesInstance { get; set; }
Here I solved the problem by simply referencing the movie object towards it's join/relational table在这里,我通过简单地将电影对象指向它的连接/关系表来解决这个问题
var movie = new Movies();
movie.MovieName = viewModel.MovieName;
movie.MovieBio = viewModel.MovieBio;
movie.MoviePosterUrl = viewModel.MoviePosterUrl;
Towards the reference navigation key which is present in the join table/navigational table like this指向存在于连接表/导航表中的参考导航键,如下所示
cinemaShowtime.CinemaInstance = cinema;
cinemaShowtime.MoviesInstance = movie;
Also the system was asking to make the composite key nullable which is present in Movie Table table like this此外,系统还要求使电影表表中存在的复合键可以为空,如下所示
public int? IndustryId { get; set; }
It should be noted that I am making many-to-many relationship same thing would happen for cinema table应该注意的是,我正在建立多对多的关系,电影桌也会发生同样的事情
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.