在Entity Framework中从数据库中删除记录时出错

时间:2014-09-14 17:58:38

标签: asp.net-mvc entity-framework asp.net-mvc-4 entity-framework-4 entity-framework-5

我正在尝试从数据库表中删除记录但是我收到此错误

  

DELETE语句与REFERENCE约束“FK_dbo.TicketDetail_dbo.Person_PersonID”冲突。冲突发生在数据库“AbcDB”,表“dbo.TicketDetail”,列'PersonID'

我正在分享我的代码,请指导我。

TicketDetail

public class TicketDetail
{
    [Key]
    public int TicketDetailId { get; set; }
    public int? GenericOrderId { get; set; }
    public int? PartId { get; set; }
    public int? JunkPartId { get; set; }
    public int PersonID { get; set; }

    [Timestamp]
    public byte[] RowVersion { get; set; }

    public virtual Part Part { get; set; }
    public virtual Ticket Ticket { get; set; }
    public virtual Customer Customer { get; set; }
    public virtual JunkPart JunkPart { get; set; }
    public virtual ICollection<MailSystem> MailSystems { get; set; }
    public virtual ICollection<MailLog> MailLogs { get; set; }

}

Person

public abstract class Person
{
    [Key]
    public int PersonID { get; set; }          
    public string City { get; set; }
    public DateTimeOffset DateAdded { get; set; }
}

Customer

public class Customer : Person
{
    public int? CountryId { get; set; }
    public string Company { get; set; }
    public virtual ICollection<OrderDetail> OrderDetails { get; set; }
    public virtual ICollection<TicketDetail> TicketDetails { get; set; }
    public virtual ICollection<MailSystem> MailSystems { get; set; }
    public virtual ICollection<MailLog> MailLogs { get; set; }
    public virtual Country Country { get; set; }
}

迁移代码:

modelBuilder.Entity<Customer>()
                .HasMany<TicketDetail>(c => c.TicketDetails)
                .WithRequired(x => x.Customer)
                .WillCascadeOnDelete(true);

当我尝试删除任何customerId时,会发生错误。

我搜索过互联网但找不到合适的解决方案,请帮我解决此错误。

0 个答案:

没有答案