在分配EntityState.Modified时出现“发生参照完整性约束违规”

时间:2012-07-31 00:43:38

标签: asp.net-mvc-3 entity-framework ef-code-first

以下是我的代码优先数据模型:

    public class Job
    {
        public Job()
        {
            this.Jobs = new List<Job>();
            this.Quotes = new List<Quote>();
            this.WorkTimes = new List<WorkTime>();
        }

        public long ID { get; set; }
        public string JobName { get; set; }
        public string Description { get; set; }

        [ForeignKey("Customer")]
        public Nullable<long> CustomerID { get; set; }
        public Nullable<decimal> LimitHours { get; set; }

        [ForeignKey("ParentJob")]
        public Nullable<long> ParentID { get; set; }
        public Nullable<System.DateTime> HardDeadline { get; set; }
        public Nullable<System.DateTime> SoftDeadline { get; set; }
        public int Priority { get; set; }
        public Nullable<byte> Progress { get; set; }

        [ForeignKey("JobState")]
        public Nullable<byte> JobStateID { get; set; }

        public virtual Customer Customer { get; set; }
        public virtual ICollection<Job> Jobs { get; set; }
        public virtual Job ParentJob { get; set; }
        public virtual ICollection<Quote> Quotes { get; set; }
        public virtual ICollection<WorkTime> WorkTimes { get; set; }
        public virtual ENUM_JobState JobState { get; set; }
    }

public class Customer
{
        public Customer()
        {
            this.Customers = new List<Customer>();
            this.Jobs = new List<Job>();
        }

   public long ID { get; set; }
   public string Name { get; set; }
   public Nullable<long> ParentID { get; set; }
   public string InvoiceEmail { get; set; }
   public virtual ICollection<Customer> Customers { get; set; }
   public virtual Customer ParentCustomer { get; set; }
   public virtual ICollection<Job> Jobs { get; set; }
}


public class ENUM_JobState
{
        public ENUM_JobState()
        {
            this.Jobs = new List<Job>();
        }

        public byte ID { get; set; }
        [Required]
        public string Value { get; set; }

        public virtual ICollection<Job> Jobs { get; set; }
}


public class ENUM_JobStateMap : EntityTypeConfiguration<ENUM_JobState>
    {
        public ENUM_JobStateMap()
        {
            // Primary Key
            this.HasKey(t => t.ID);

            // Properties
            this.Property(t => t.Value)
                .HasMaxLength(100);

            // Table & Column Mappings
            this.ToTable("ENUM_JobStates");
            this.Property(t => t.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired(); 
            this.Property(t => t.Value).HasColumnName("Value");


        }
    }

public class JobMap : EntityTypeConfiguration<Job>
    {
        public JobMap()
        {
            // Primary Key
            this.HasKey(t => t.ID);

            // Properties
            this.Property(t => t.JobName)
                .HasMaxLength(50);

            this.Property(t => t.Description)
                .HasMaxLength(100);

            // Table & Column Mappings
            this.ToTable("Jobs");
            this.Property(t => t.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();
            this.Property(t => t.JobName).HasColumnName("JobName");
            this.Property(t => t.Description).HasColumnName("Description");
            this.Property(t => t.CustomerID).HasColumnName("CustomerID");
            this.Property(t => t.JobStateID).HasColumnName("JobStateID");
            this.Property(t => t.LimitHours).HasColumnName("LimitHours");
            this.Property(t => t.ParentID).HasColumnName("ParentID");
            this.Property(t => t.HardDeadline).HasColumnName("HardDeadline");
            this.Property(t => t.SoftDeadline).HasColumnName("SoftDeadline");
            this.Property(t => t.Priority).HasColumnName("Priority");
            this.Property(t => t.Progress).HasColumnName("Progress");

            // Relationships
            this.HasOptional(t => t.Customer)
                .WithMany(t => t.Jobs)
                .HasForeignKey(d => d.CustomerID);
            this.HasOptional(t => t.ParentJob)
                .WithMany(t => t.Jobs)
                .HasForeignKey(d => d.ParentID);


            this.HasRequired(t => t.JobState)
                .WithMany(t => t.Jobs)
                .HasForeignKey(t => t.JobStateID);
              //.HasForeignKey(d => d.JobStateID);

        }
    }

public class CustomerMap : EntityTypeConfiguration<Customer>
{
    public CustomerMap()
    {
        // Primary Key
        this.HasKey(t => t.ID);

        // Properties
        this.Property(t => t.Name)
            .HasMaxLength(80);

        this.Property(t => t.InvoiceEmail)
            .HasMaxLength(80);

        // Table & Column Mappings
        this.ToTable("Customers");
        this.Property(t => t.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired(); 
        this.Property(t => t.Name).HasColumnName("Name");
        this.Property(t => t.ParentID).HasColumnName("ParentID");
        this.Property(t => t.InvoiceEmail).HasColumnName("InvoiceEmail");

        // Relationships
        this.HasOptional(t => t.ParentCustomer)
            .WithMany(t => t.Customers)
            .HasForeignKey(d => d.ParentID);

    }
}

发生错误的我的MVC3控制器:

    [HttpPost]
    public ActionResult Edit(Job job)
    {
        if (ModelState.IsValid)
        {
            if (job.ParentJob != null || string.IsNullOrWhiteSpace(job.ParentJob.JobName))
                job.ParentJob = null;
            job.JobState = ENUM_JobState.CreateIfNotExist2(job.JobState.Value, db);
            job.Customer = Customer.CreateIfNotExist2(job.Customer.Name, db);

            //db.Jobs.Attach(job);
            db.Entry(job).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(job);
    }

我正在使用AJAX自动完成功能,CreateIfNotExist2函数会查找键入的值,或者如果它们不存在则创建它们。

db.Entry(job).State = EntityState.Modified;行引发了异常:

A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.

如此通用的异常,这并不好笑。我在这里和其他地方试过很多明显的解决方案。

我很难过。

1)问题是什么? 2)如果不存在实体,是否有更好的MVC / EF方式来创建实体,选择之后?

0 个答案:

没有答案