2017-05-07 116 views
0

我正在与具有1对多关系的两个实体County和Patient一起工作。实体框架6中的多重关系变化从1对多到1对0或0..1

public class County 
{ 
    public int CountyId { get; set; } // Primary Key 
    public string CountyName { get; set;) // A unique index column 
    public virtual ICollection<Patient> Patients { get; set; } 
} 

public class CountyMap : EntityTypeConfiguration<County> 
{ 
    public CountyMap() 
    { 
     ToTable("Counties"); 
     HasKey(c => c.CountyId); 
     Property(c => c.CountyId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 
      Property(c => c.CountyName).IsRequired().HasMaxLength(50).HasColumnAnnotation("Index", 
       new IndexAnnotation(new IndexAttribute("IX_Counties", 1) { IsUnique = true })); 
    } 
} 

public class Patient 
{ 
    public int PatientId { get; set; } 
    public string PatientLastName { get; set; } 
    public string PatientFirstName { get; set; } 
    public string CountyName { get; set; } 
    public int CountyId { get; set; } // Foreign key to Counties table 
    public virtual County County { get; set; } // Navigation property 
} 

public class PatientMap: EntityTypeConfiguration<Patient> 
{ 
    public PatientMap() 
    { 
      ToTable("Patients"); 
      HasKey(p => p.PatientId); 
      Property(p => p.PatientId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 
      Property(p => p.PatientLastName).IsRequired().HasMaxLength(50); 
      Property(p => p.PatientFirstName).IsRequired().HasMaxLength(50); 
      Property(p => p.CountyId).IsRequired(); 
      HasRequired(p => p.County).WithMany(c => c.Patients).HasForeignKey(p => p.CountyId); 
    } 
} 

public class AppContext : DbContext 
{ 
    public AppContext() 
     : base("name=AppContext") 
    { 
    } 

    public virtual DbSet<County> Counties { get; set; } 
    public virtual DbSet<Patient> Patients { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Configurations.Add(new CountyMap()); 
     modelBuilder.Configurations.Add(new PatientMap()); 
    } 
} 

public class PatientUOW 
{ 
    public Patient CreatePatient(Patient patient) 
    { 
     string errorMessage = String.Empty; 
     Patient patientReturned = null; 
     County county = null; 

     try 
     { 
      using (AppContext ctx = new AppContext()) 
      { 
       // ONLY Pre-existing counties are permitted 
       county = ctx.Counties.Where(c => c.CountyName == patient.CountyName).SingleOrDefault<County>(); 

       county.Patients.Add(patient); 
       ctx.SaveChanges(); // An exception is thrown here 

      } 
     } 
     catch (Exception err) 
     { 
     } 
    } 
} 

异常消息是:

多重约束侵犯。 角色'Patient_County_Target'的关系'ArielOperations.Domain.Concrete.Patient_County'具有 多重性1或0..1。

在县实体的调试器显示:

enter image description here

enter image description here

谁能解释这里发生了什么?我在这里和其他地方看到了几个条目,而且没有一条似乎有效。

谢谢。

+0

为什么'Patient'有'CountyName'?这是多余的。 –

回答

0

我能解决我的问题。关键似乎是为了避免在县中添加患者时尝试创建新的县记录。为了实现这一点,我没有将County实例传递给CreatePatient方法。新患者只包含目标县的CountyId。

Patient newPatient = new Patient 
{ 
    PatientLastName = "Doe", 
    PatientFirstName = "Jane", 
    CountyName = "Denton", 
    CountyId = 4 
}; 

我们可以在这个newPatient实例现在传递给CreatePatient方法。

public Patient CreatePatient(Patient patient) 
{ 
    string errorMessage = String.Empty; 
    Patient patientReturned = null; 
    County county = null; 

    try 
    { 
      using (AppContext ctx = new AppContext()) 
      { 
       // ONLY Pre-existing counties are permitted 
       county = ctx.Counties.Where(c => c.CountyName == patient.CountyName).SingleOrDefault<County>(); 
       ctx.Patients.Add(patient); 
       ctx.SaveChanges(); 
       patientReturned = patient; 
      } 
    } // end try 
    catch (Exception err) 
    { 
     errorMessage = err.Message; 
    } // end catch (Exception err) 

    return patientReturned; 
} // end public Patient CreatePatient(Patient patient) 

这似乎工作,甚至通过外键将患者连接到县记录。看来EF在这里做了一些事情来达到预期目的。