【发布时间】:2011-11-03 05:28:15
【问题描述】:
在 SaveChanges() 方法期间,我遇到了来自 EF 的以下异常。
违反了多重性约束。关系“CodeFirstNamespace.Person_Phones”的角色“Person_Phones_Source”具有多重性 1 或 0..1。
我的映射似乎是正确的,因为我可以选择并且所有相关对象都通过连接正确填充。我已经包含了有关表、映射和调用代码的信息。任何帮助将不胜感激。
表格:
Person(personguid,firstname,lastname,etc...)
Person_Phone(personguid,phoneguid,CreatedBy,etc...)
电话(phoneguid、电话号码等...)
编辑:根据要求,这些是我的实体。为简洁起见,我删除了修复代码。代理生成已禁用。
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();
}
【问题讨论】:
-
同时显示您的实体。
-
我已经添加了相关实体。
-
这个问题你解决了吗?