I am having trouble with the following exception from EF during the SaveChanges() method.
Multiplicity constraint violated. The role 'Person_Phones_Source' of the relationship 'CodeFirstNamespace.Person_Phones' has multiplicity 1 or 0..1.
My mappings seem to be correct, as I can select and all the related objects are correctly populated via the joins. I've included information regarding the tables, the mappings, and the calling code. Any help would be greatly appreciated.
Tables:
Person(personguid,firstname,lastname,etc...)
Person_Phone(personguid,phoneguid,CreatedBy,etc...)
Phone(phoneguid,phonenumber,etc...)
Edit: As requested these are my entities. I have removed fixup code for brevity. Proxy generation is disabled.
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);
}
}
And the Calling code:
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 开发者_Go百科=> 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();
}
You have defined an intermediate join table (Person_Phones) between your Person and Phones table. The problem is that you try to define a foreign key on the association between Person and Person_Phones - the PersonID is not the key on that table as it has a compound key of PersonID, PhoneID.
You could try removing the relationship declaration from your person map (or at least the HasForeignKey declaration)
精彩评论