0

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

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

代码 :

用户.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();
    }
}

调用.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它时,它会删除所有Callsfor the User,这反过来又会删除所有ETAsfor those Calls

当我从数据库中删除用户行(使用数据库资源管理器)时,它给了我一个错误:

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

4

2 回答 2

0

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

于 2013-01-21T16:54:13.770 回答
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();
    }
}
于 2013-01-22T10:52:14.303 回答