与级联给出错误的一对多关系

时间:2013-01-21 16:43:11

标签: entity-framework ef-code-first entity-relationship cascading-deletes ef-migrations

我正在学习迁移的EF Code First,我有3个实体:

[User] 1--->* [Call] 1--->* [ETA]

代码:

User.cs

public class User
{
    [Key]
    public int Id { get; set; }

    public Guid LongId { get; set; }

    [Required]
    public string Name { get; set; }

    public virtual ICollection<Call> Calls { get; set; } // many calls

    public User()
    {
        LongId = Guid.NewGuid();
    }
}

Call.cs

public class Call
{
    [Key]
    public int Id { get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public string BreakdownNo { get; private set; }

    [Required,MaxLength(32)]
    public string Customer { get; set; }

    [Required,MaxLength(32)]
    public string TrailerNo { get; set; }

    [Required, MaxLength(32)]
    public string DepotContact { get; set; }

    [Required, MaxLength(48), RegularExpression(@"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$")]
    public string DepotEmail { get; set; }

    [Required, MinLength(9), MaxLength(32)]
    public string DepotPhone { get; set; }

    [Required, MaxLength(32)]
    public string DriverContact { get; set; }

    [Required, MinLength(9), MaxLength(32), RegularExpression(@"^(7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$")]
    public string DriverPhone { get; set; }

    [Required, MaxLength(256)]
    public string LocatedAtFreeText { get; set; }

    [Required, MaxLength(8), RegularExpression(@"^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {0,1}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$")]
    public string LocatedAtPostCode { get; set; }

    [Required, MaxLength(16)]
    public string StartupNo { get; set; }

    [Required]
    public bool IsLoaded { get; set; }

    [Required, MaxLength(256)]
    public string FaultDescription { get; set; }

    [Required]
    public DateTime StartTime { get; set; }

    public DateTime? EndTime { get; set; }
    public string Status { get; set; }

    public virtual User Controller { get; set; } // 1 controller
    public virtual ICollection<ETA> ETAs { get; set; } // many ETAs

    public Call()
    {
        StartTime = DateTime.Now;
        ETAs = new List<ETA> { new ETA() };
        Status = "Logged";
    }
}

ETA.c

public class ETA
{
    [Key]
    public int Id { get; set; }

    [Required]
    public TimeSpan Value { get; set; }

    public int CallId { get; set; }

    public ETA()
    {
        Value = TimeSpan.FromMinutes(90);
    }
}

我希望如此,当我删除User时,它会删除Calls的所有UserETAs会删除Calls的所有{{1}} {1}}。

当我从数据库中删除用户行时(使用数据库浏览器),它给出了一个错误:

没有删除任何行。 尝试删除第201行时出现问题。 错误来源:.Net SqlClient数据提供程序。 错误消息:DELETE语句与REFERENCE约束“FK_dbo.Calls_dbo.Users_Controller_Id”冲突。冲突发生在数据库“BreakdownDb”,表“dbo.Calls”,列'Controller_Id'。

2 个答案:

答案 0 :(得分:0)

您可以在实体框架中启用级联删除选项,在这里您可以找到更多信息: http://blogs.msdn.com/b/alexj/archive/2009/08/19/tip-33-how-cascade-delete-really-works-in-ef.aspx

答案 1 :(得分:0)

解决方案是将OnModelCreating方法添加到我的DbContext类:

public class BreakdownDb : DbContext
{
    public DbSet<Call> Calls { get; set; }
    public DbSet<User> Users { get; set; }

    public BreakdownDb(): base("name=DefaultConnection") {}

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>().HasMany(x => x.Calls).WithRequired();
        modelBuilder.Entity<Call>().HasMany(x => x.ETAs).WithRequired();
    }
}