我在SaveChanges()方法中遇到以下來自EF的異常問題。實體框架4.1流利映射(關聯表)
違反了多重性約束。關係「CodeFirstNamespace.Person_Phones」的角色「Person_Phones_Source」具有多重性1或0..1。
我的映射似乎是正確的,因爲我可以選擇並通過連接正確地填充所有相關的對象。我已經包含了有關表格,映射和調用代碼的信息。任何幫助將不勝感激。
表:
人(personguid,名字,姓氏,等...)
Person_Phone(personguid,phoneguid,CreatedBy等)
電話(phoneguid,PHONENUMBER,等等......)
編輯:按要求這些是我的實體。爲了簡潔起見,我刪除了fixup代碼。代理生成被禁用。
public partial class Person
{
public virtual System.Guid PersonId { get; set;}
public virtual string LastName { get; set; }
public virtual string FirstName{ get; set; }
public virtual ObservableCollection<PersonPhoneAssociation> Phones {get;set;}
}
public partial class PersonPhoneAssociation
{
public virtual System.Guid PersonId {get;set;}
public virtual System.Guid PhoneId {get;set;}
public virtual string CreatedBy {get;set;}
public virtual Person Person {get;set;}
public virtual Phone Phone {get;set;}
}
public partial class Phone
{
public virtual System.Guid PhoneId { get; set; }
public virtual string PhoneNumber {get; set; }
public virtual ObservableCollection<PersonPhoneAssociation> People {get;set;}
}
public class PersonMap : EntityTypeConfiguration<Person>
{
public PersonMap()
{
// Primary Key
this.HasKey(t => t.PersonId);
// Properties
this.Property(t => t.LastName).IsRequired().HasMaxLength(64);
this.Property(t => t.FirstName).IsRequired().HasMaxLength(64);
// Table & Column Mappings
this.ToTable("Person");
this.Property(t => t.PersonId).HasColumnName("personguid");
this.Property(t => t.LastName).HasColumnName("lastname");
this.Property(t => t.FirstName).HasColumnName("firstname");
// Relationships
this.HasMany(i => i.Phones).WithRequired(t => t.Person).HasForeignKey(t => t.PersonId);
}
}
public class PhoneMap : EntityTypeConfiguration<Phone>
{
public PhoneMap()
{
// Primary Key
this.HasKey(t => t.PhoneId);
// Properties
// Table & Column Mappings
this.ToTable("Phone");
this.Property(t => t.PhoneId).HasColumnName("phoneguid");
this.Property(t => t.PhoneNumber).HasColumnName("phonenumber");
}
}
public class PersonPhoneAssociationMap : EntityTypeConfiguration<PersonPhoneAssociation>
{
public PersonPhoneAssociationMap()
{
// Primary Key
this.HasKey(t => new { t.PersonId, t.PhoneId });
// Properties
this.Property(t => t.PersonId).IsRequired();
this.Property(t => t.PhoneId).IsRequired();
this.Property(t => t.CreatedBy).HasMaxLength(64);
// Table & Column Mappings
this.ToTable("Person_Phone");
this.Property(t => t.PersonId).HasColumnName("personguid");
this.Property(t => t.PhoneId).HasColumnName("phoneguid");
this.Property(t => t.CreatedBy).HasColumnName("CreatedBy");
// Relationships
this.HasRequired(t => t.Person)
.WithMany(t => t.Phones)
.HasForeignKey(t => t.PersonId);
this.HasRequired(t => t.Phone)
.WithMany(t => t.People)
.HasForeignKey(d => d.PhoneId);
}
}
,並且調用代碼:
using (var context = new EnterpriseContext())
{
System.Guid personId = new System.Guid("417B85E7-19C4-4C61-A9C2-627C2A0C5C85");
var person = context.Set<Person>()
.Include(t => t.Phones.Select(p => p.Person))
.Include(t => t.Phones.Select(p => p.Phone))
.Where(p => p.PersonId == personId).FirstOrDefault();
Phone phone = new Phone() { PhoneNumber = "8675309" };
PersonPhoneAssociation pfa = new PersonPhoneAssociation() { Phone = phone };
person.Phones.Add(pfa);
context.SaveChanges();
}
也顯示您的實體。 –
我已添加相關實體。 – RMCQUEEN
你解決了這個問題嗎? –