在asp.net mvc中更新数据库时出现外键错误

时间:2014-07-14 08:45:01

标签: c# entity-framework asp.net-mvc-4

我正在尝试建立简单的一对多和多对多关系并在表中插入记录。但每当我更新数据库时,我都会收到此错误 -

  

System.Data.Entity.Infrastructure.DbUpdateException:更新条目时发生错误。有关详细信息,请参阅内部异常---> System.Data.UpdateException:更新条目时发生错误。有关详细信息,请参阅内部异常---> System.Data.SqlClient.SqlException:UPDATE语句与FOREIGN KEY约束“FK_dbo.Movies_dbo.Producers_ProducerId”冲突。冲突发生在数据库“aspnet-MvcApplication1-20140713210255”,表“dbo.Producers”,列'ProducerId'。

以下是我的模型类 -

Movie.cs

public class Movie
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int MovieId { get; set; }
    public string Name { get; set; }
    public string YearOfRelease { get; set; }
    public string Plot { get; set; }
    public string ImgUrl { get; set; }

    public int ProducerId { get; set; }

    public virtual Producer Producer { get; set; }
    public virtual ICollection<Actor> Actors { get; set; }
    public virtual ICollection<MovieReview> Reviews { get; set; }

}

Actor.cs

public class Actor
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ActorId { get; set; }
    public string Name { get; set; }
    public string Sex { get; set; }
    public string DOB { get; set; }
    public string BioData { get; set; }

    public virtual ICollection<Movie> Movies { get; set; }

    [Display(Name = "User Name")]
    [DisplayFormat(NullDisplayText = "anonymous")]
    public virtual UserProfile ApplicationUser { get; set; }

}

Producer.cs

public class Producer
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ProducerId { get; set; }
    public string Name { get; set; }
    public string Sex { get; set; }
    public string DOB { get; set; }
    public string BioData { get; set; }

    public virtual ICollection<Movie> Movies { get; set; }

    [Display(Name = "User Name")]
    [DisplayFormat(NullDisplayText = "anonymous")]
    public virtual UserProfile ApplicationUser { get; set; }
}

这是我的配置文件 -

context.Movies.AddOrUpdate(
            p => p.Name,
            new Movie { Name = "Shawshank Redemption", Plot = "Wrongly convicted for the crime he didn't commit, finds redemption and more in the jail.", YearOfRelease = "1994-5-23", Actors = new List<Actor>()},
            new Movie { Name = "Saving Private Ryan", Plot = "Finding a lost war man.", YearOfRelease = "1998-2-11", Actors = new List<Actor>()}
            );
        context.SaveChanges();

        context.Actors.AddOrUpdate(
            p => p.Name,
            new Actor { Name = "Tim Robbins", Sex = "Male" },
            new Actor { Name = "Tom Hanks", Sex = "Male" }
            );
        context.SaveChanges();

        context.Producers.AddOrUpdate(
            p => p.Name,
            new Producer { Name = "Niki Marvin" },
            new Producer { Name = "Steven Spielberg" }
            );
        context.SaveChanges();

        var movie = context.Movies.SingleOrDefault(m => m.Name == "Shawshank Redemption");
        var actor = context.Actors.SingleOrDefault(a => a.Name == "Tim Robbins");
        movie.Actors.Add(actor);
        var producer = context.Producers.SingleOrDefault(p => p.Name == "Niki Marvin");
        movie.Producer = producer;

        movie = context.Movies.SingleOrDefault(m => m.Name == "Saving Private Ryan");
        actor = context.Actors.SingleOrDefault(a => a.Name == "Tom Hanks");
        movie.Actors.Add(actor);
        producer = context.Producers.SingleOrDefault(p => p.Name == "Steven Spielberg");
        movie.Producer = producer;

        context.SaveChanges();

我在这里做错了什么?上面的数据插入是否正确?任何形式的帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

当您insertupdate电影时,您需要在ProducerId表中设置有效ProducerId,这是Producers。如果传递表中不存在的值,则insertupdate将失败,因为引用的表中不存在该值。

如果movie不必拥有producer,那么您可以将ProducerId表/型号的Movies设置为nullabledatabase scheme和您的Movie模型中。