以下は私のコードファーストのデータモデルです:
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方法はありますか?