SaveChanges() 메서드를 실행하는 동안 EF에서 다음 예외를 사용하는 데 문제가 있습니다.Entity Framework 4.1 Fluent Mapping (연결 테이블)
다중성 제약 조건 위반. 'CodeFirstNamespace.Person_Phones'관계의 'Person_Phones_Source'역할의 다중도는 1 또는 0..1입니다.
내가 선택할 수 있고 모든 관련 개체가 조인을 통해 올바르게 채워지므로 내 매핑이 올바른 것처럼 보입니다. 테이블, 매핑 및 호출 코드에 관한 정보를 포함했습니다. 어떤 도움이라도 대단히 감사하겠습니다.
테이블 :
사람 (personguid, 이름, 성 등)
Person_Phone (personguid, phoneguid, CreatedBy 등)
전화 (phoneguid, PHONENUMBER, 기타 ...)
편집 : 요청한대로 내 항목이 있습니다. 간결함을 위해 픽스 업 코드를 제거했습니다. 프록시 생성이 비활성화됩니다.
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);
}
}
그리고 호출 코드 : 당신은 당신의 사람과 전화 테이블 사이의 중간 조인 테이블 (Person_Phones)를 정의
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
이 문제가 해결 되었습니까? –