实体框架4.1流畅映射(关联表)

时间:2011-08-24 22:28:10

标签: entity-framework-4.1

在SaveChanges()方法期间,我遇到了来自EF的以下异常。

  

违反了多重性约束。 “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);

    }
}

和来电代码:

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();
        }

1 个答案:

答案 0 :(得分:0)

您已在Person和Phones表之间定义了一个中间连接表(Person_Phones)。问题是您尝试在Person和Person_Phones之间的关联上定义外键 - PersonID不是该表上的键,因为它具有PersonID,PhoneID的复合键。

您可以尝试从人员地图中删除关系声明(或至少从HasForeignKey声明中删除)

相关问题