使用Fluent API EF Code First约束问题

时间:2015-09-20 08:33:42

标签: c# c#-4.0 entity-framework-6 entity-framework-core

我有两节课。

public class Om_MembershipCharges
{
    [Key]
    public Int32 MembershipChargesID { get; set; }
    public Decimal Amount { get; set; }
    public Int16 PerMonth { get; set; }
    public Int16? MemebershipTypeID { get; set; }

    public virtual Om_MembershipType MemebershipType { get; set; }
}

public class Om_MembershipType
{
    [Key]
    public Int16 MemebershipTypeID { get; set; }
    public String MemebershipType { get; set; }
    public Boolean IsDefaultMembership { get; set; }

    public virtual ICollection<Om_MembershipCharges> MembershipCharges { get; set; }
}

数据库模型如下。

public MembershipTypeMap()
{
    Property(x => x.MemebershipTypeID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    Property(x => x.MemebershipType)
        .IsRequired()
        .HasMaxLength(100)
        .HasColumnAnnotation
        (
            IndexAnnotation.AnnotationName,
            new IndexAnnotation
            (
                new IndexAttribute("U_MembershipType", 1) { IsUnique = true }
            )
        );

    Property(x => x.IsDefaultMembership).IsRequired();
    ToTable(clsCommon.tblMembershipType);
}


public class MembershipChargesMap : EntityTypeConfiguration<Om_MembershipCharges>
{
    public MembershipChargesMap()
    {
        Property(x => x.MembershipChargesID);

        Property(x => x.Amount).IsRequired();
        Property(x => x.PerMonth).IsRequired();
        Property(x => x.MemebershipTypeID).IsOptional();    
        ToTable(clsCommon.tblMembershipCharges);
    }
}

以下是约束

modelBuilder.Entity<Om_MembershipCharges>()
       .HasOptional(x => x.MemebershipType)
       .WithMany().HasForeignKey(x => x.MemebershipTypeID)
       .WillCascadeOnDelete(false);


modelBuilder.Entity<Om_MembershipCharges>()
    .HasRequired(m => m.MemebershipType).WithMany(m => m.MembershipCharges)
    .HasForeignKey(m => m.MembershipChargesID).WillCascadeOnDelete(false);

我面临下面的异常以前有人遇到过这类问题吗?

  

在模型生成期间检测到一个或多个验证错误:

     

Om_MembershipCharges_MemebershipType_Source ::多重性不是   在角色和Om_MembershipCharges_MemebershipType_Source&#39;中有效在   关系&#39; Om_MembershipCharges_MemebershipType&#39;。因为   依赖角色是指关键属性,即上界   依赖角色的多样性必须是&#39; 1&#39;。   Om_MembershipCharges_MemebershipType_Target_Om_MembershipCharges_MemebershipType_Source:   :参照的从属角色中的所有属性的类型   约束必须与相应的属性类型相同   主要角色。财产的类型&#39; MembershipChargesID&#39;在实体上   &#39; Om_MembershipCharges&#39;与财产类型不匹配   &#39; MemebershipTypeID&#39;实体&#39; Om_MembershipType&#39;在参考   约束&#39; Om_MembershipCharges_MemebershipType&#39;。

1 个答案:

答案 0 :(得分:1)

您的上一个代码段...

modelBuilder.Entity<Om_MembershipCharges>()
    .HasRequired(m => m.MemebershipType).WithMany(m => m.MembershipCharges)
    .HasForeignKey(m => m.MembershipChargesID).WillCascadeOnDelete(false);

...覆盖m.MembershipType的先前注册,现在使其成为必需,EF表示其多样性应为1,而不是0..1

看到导航属性,映射应该是最后两个片段的合并(请注意HasOptional)。 您应该使用MembershipTypeID代替MembershipChargesID

modelBuilder.Entity<Om_MembershipCharges>()
    .HasOptional(m => m.MemebershipType).WithMany(m => m.MembershipCharges)
    .HasForeignKey(m => m.MembershipTypeID).WillCascadeOnDelete(false);

旁注:我会将所有映射代码放入映射类中。维护更容易将它们保持在一起。我认为您甚至还需要这样做,因为除了EntityTypeConfiguration注册类型外,EF不会允许您注册modelBuilder.Entity<T>