0

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

    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 方法来创建实体,然后选择?

4

0 回答 0