0

我在不违反外键约束的情况下删除 EF Code First(新数据库)中的对象时遇到问题。

我有三个类,Job、Ticket 和 Notes。Job 有 0,1 个票据和许多注释。我想用 db.Job.Remove(job) 删除一个作业(及其对应的票证/注释),但这会导致 Job 和 Ticket 之间的 FK 冲突。我应该如何构造/注释类?

提前致谢。
以下片段定义了对象/关系:

public WorkflowEntities() : base("CodeFirstWorkflow") { }

    public DbSet<Job> Job { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
        base.OnModelCreating(modelBuilder);

        // Should create a 1:0,1 between Job and Ticket with ticket being optional
        modelBuilder.Entity<Ticket>()
            .HasRequired(e => e.Job)
            .WithOptional(e => e.Ticket);
    }
}

public class Job {
    public int JobId { get; set; }

    public virtual Ticket Ticket { get; set; }
    public virtual ICollection<Notes> Notes { get; set; }
}

public class Ticket {
    public int TicketId { get; set; }
    public int JobId { get; set; }

    public virtual Job Job { get; set; }
}

public class Notes {
    public int NotesId { get; set; }
    public int JobId { get; set; }

    public virtual Job Job { get; set; }
}
4

1 回答 1

0

Job侧面配置:

modelBuilder.Entity<Job>()
.HasMany(p => p.Ticket)
.WithRequired()
.HasForeignKey(c => c.JobId)
.WillCascadeOnDelete(true);
于 2013-04-17T01:44:36.560 回答