简体   繁体   English

如何解决 System.Data.Entity.Infrastructure.DbUpdateException

[英]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.

相关问题 System.Data.Entity.Infrastructure.DbUpdateException - System.Data.Entity.Infrastructure.DbUpdateException System.Data.Entity.Infrastructure.DbUpdateException问题 - System.Data.Entity.Infrastructure.DbUpdateException Issue 发生System.Data.Entity.Infrastructure.DbUpdateException - System.Data.Entity.Infrastructure.DbUpdateException occurred 如何避免System.Data.Entity.Infrastructure.DbUpdateException - How to avoid System.Data.Entity.Infrastructure.DbUpdateException MVC EF-System.Data.Entity.Infrastructure.DbUpdateException - MVC EF - System.Data.Entity.Infrastructure.DbUpdateException 递归树方法上的System.Data.Entity.Infrastructure.DbUpdateException - System.Data.Entity.Infrastructure.DbUpdateException on recursive Tree method 将数据添加到数据库(错误:System.Data.Entity.Infrastructure.DbUpdateException) - Adding Data to Database (Error: System.Data.Entity.Infrastructure.DbUpdateException) EntityFramework.dll 中发生“System.Data.Entity.Infrastructure.DbUpdateException” - 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll 发生类型为“ System.Data.Entity.Infrastructure.DbUpdateException”的异常 - An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred 类型为“ System.Data.Entity.Infrastructure.DbUpdateException”的异常 - An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM