ForeignKey Mapping不适用于EntityFramework 5

时间:2014-04-18 17:40:32

标签: c# asp.net asp.net-mvc entity-framework c#-4.0

ActionItem.ActionItemId是ActionItemsToSource数据库中的FK。我有以下内容:

ActionItem.cs:

public partial class ActionItem : BaseModel
{
    public ActionItem()
    {
    }

    public Guid ActionItemId { get; set; }

    public Guid AccountId { get; set; }

    [MaxLength(50)]
    [Required]
    public string Title { get; set; }

    [MaxLength(1200)]
    [Required]
    public string Description { get; set; }

    /// <summary>
    /// Action Code is read only
    /// </summary>
    public string ActionCode 
    { 
        get 
        {
            if (ComplianceCategory != null)       
            {
                return (ComplianceCategory.Code ?? String.Empty) + "-" + Sequence.ToString("0000");
            }
            return String.Empty;
        } 
    }

    /// <summary>
    /// Note: this is only "set" during "insert" (never changed on update)
    ///       This sequence is autocalculated in "CreateActionItem" API method
    /// </summary>
    public int Sequence { get; set; }

    [Required]
    public Guid ComplianceCategoryId { get; set; }

    public CodesLookup ComplianceCategory { get; set; }

    public Guid? PermitId { get; set; }

    public Guid? RuleId { get; set; }

    public CodesLookup FrequencyType { get; set; }

    public Guid? FrequencyTypeId { get; set; }

    public int? CategorySequence { get; set; }

    [MaxLength(60)]
    public string ActionItemCitation { get; set; }

    [MaxLength(60)]
    public string SupportingCitation1 { get; set; }

    [MaxLength(60)]
    public string SupportingCitation2 { get; set; }

    [MaxLength(60)]
    public string SupportingCitation3 { get; set; }

    public DateTime? InitialDueDate { get; set; }

    [MaxLength(200)]
    public string InitialDueDateDescription { get; set; }

    public DateTime? ComplianceDate { get; set; }

    public bool? IsRecurring { get; set; }

    public CodesLookup TaskFrequencyType { get; set; }

    public Guid? TaskFrequencyTypeId { get; set; }

    public int? RuntimeRecurrence { get; set; }

    public CodesLookup DueDateType { get; set; }

    public Guid? DueDateTypeId { get; set; }

    [MaxLength(300)]
    public string Notes { get; set; }

    public int? ActionItemNumber { get; set; }

    public Guid? ApplicabilityClassTypeId { get; set; }

    public CodesLookup ApplicabilityClass { get; set; }

    public Guid? ManagedByUserId { get; set; }
    public Guid? AssignedToUserId { get; set; }

    public bool AreTasksAutoAccepted { get; set; }

    public bool IncludeSourceEmissionLimits { get; set; }

    public OrgContact ManagedByUser { get; set; }

    public OrgContact AssignedToUser { get; set; }

    public ActionItemsToSource ActionItemsToSource { get; set; }
}

ActionItemsToSource.cs:

public class ActionItemsToSourceMap : EntityTypeConfiguration<ActionItemsToSource>
{
    public ActionItemsToSourceMap()
    {
        // Primary Key
        this.HasKey(t => t.ActionItemsToSourceId);
        this.Property(t => t.ActionItemsToSourceId).HasColumnName("ActionItemsToSourceId").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        // Properties
        this.Property(t => t.RecordStatus)
            .IsRequired()
            .IsFixedLength()
            .HasMaxLength(1);
        this.Property(t => t.Notes)
            .HasMaxLength(500);
        // Table & Column Mappings
        this.ToTable("ActionItemsToSource");
        this.Property(t => t.ActionItemsToSourceId).HasColumnName("ActionItemsToSourceId");
        this.Property(t => t.RecordStatus).HasColumnName("RecordStatus");
        this.Property(t => t.SourceId).HasColumnName("SourceId");
        this.Property(t => t.ActionItemId).HasColumnName("ActionItemId");
        this.Property(t => t.ActiveStatus).HasColumnName("ActiveStatus");
        this.Property(t => t.StartCountdownDate).HasColumnName("StartCountdownDate");
        this.Property(t => t.InitialTaskDueDate).HasColumnName("InitialTaskDueDate");
        this.Property(t => t.EstMonthlyAvgRuntime).HasColumnName("EstMonthlyAvgRuntime");
        this.Property(t => t.Notes).HasColumnName("Notes");
        this.Property(t => t.AccountId).HasColumnName("AccountId");
        this.Property(t => t.InsertDate).HasColumnName("InsertDate");
        this.Property(t => t.ModifiedDate).HasColumnName("ModifiedDate");
        this.Property(t => t.InsertedByUserId).HasColumnName("InsertedByUserId");
        this.Property(t => t.ModifiedByUserId).HasColumnName("ModifiedByUserId");

        // relationships

        this.HasRequired(t => t.ActionItem)
            .WithMany()
            .HasForeignKey(d => d.ActionItemId);

        this.HasRequired(t => t.Source)
            .WithMany()
            .HasForeignKey(d => d.SourceId);
    }
}

ActionItemMap.cs:

public class ActionItemMap : EntityTypeConfiguration<ActionItem>
{
    public ActionItemMap()
    {
        // Primary Key
        this.HasKey(t => t.ActionItemId);
        this.Property(t => t.ActionItemId).HasColumnName("ActionItemId").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        // Properties
        this.Property(t => t.RecordStatus)
            .IsRequired()
            .IsFixedLength()
            .HasMaxLength(1);
        this.Property(t => t.Title);
        this.Property(t => t.Description)
            .HasMaxLength(1200);
        this.Property(t => t.ActionItemCitation)
            .HasMaxLength(80);
        this.Property(t => t.SupportingCitation1)
            .HasMaxLength(2000);
        this.Property(t => t.SupportingCitation2)
            .HasMaxLength(2000);
        this.Property(t => t.SupportingCitation3)
            .HasMaxLength(2000);
        this.Property(t => t.InitialDueDateDescription)
            .HasMaxLength(2000);
        this.Property(t => t.Notes)
            .HasMaxLength(500);
        // Table & Column Mappings
        this.ToTable("ActionItem");
        this.Property(t => t.ActionItemId).HasColumnName("ActionItemId");
        this.Property(t => t.AccountId).HasColumnName("AccountId");
        this.Property(t => t.RecordStatus).HasColumnName("RecordStatus");
        this.Property(t => t.Description).HasColumnName("Description");
        this.Property(t => t.ComplianceCategoryId).HasColumnName("ComplianceCategoryId");
        this.Property(t => t.PermitId).HasColumnName("PermitId");
        this.Property(t => t.RuleId).HasColumnName("RuleId");
        this.Property(t => t.FrequencyTypeId).HasColumnName("FrequencyTypeId");
        this.Property(t => t.ActionItemCitation).HasColumnName("ActionItemCitation");
        this.Property(t => t.SupportingCitation1).HasColumnName("SupportingCitation1");
        this.Property(t => t.SupportingCitation2).HasColumnName("SupportingCitation2");
        this.Property(t => t.SupportingCitation3).HasColumnName("SupportingCitation3");
        this.Property(t => t.InitialDueDate).HasColumnName("InitialDueDate");
        this.Property(t => t.InitialDueDateDescription).HasColumnName("InitialDueDateDescription");
        this.Property(t => t.ComplianceDate).HasColumnName("ComplianceDate");
        this.Property(t => t.IsRecurring).HasColumnName("IsRecurring");
        this.Property(t => t.TaskFrequencyTypeId).HasColumnName("TaskFrequencyTypeId");
        this.Property(t => t.RuntimeRecurrence).HasColumnName("RuntimeRecurrence");
        this.Property(t => t.DueDateTypeId).HasColumnName("DueDateTypeId");
        this.Property(t => t.Notes).HasColumnName("Notes");
        this.Property(t => t.InsertDate).HasColumnName("InsertDate");
        this.Property(t => t.ModifiedDate).HasColumnName("ModifiedDate");
        this.Property(t => t.InsertedByUserId).HasColumnName("InsertedByUserId");
        this.Property(t => t.ModifiedByUserId).HasColumnName("ModifiedByUserId");
        this.Property(t => t.ActionItemNumber).HasColumnName("ActionItemNumber");
        this.Property(t => t.ApplicabilityClassTypeId).HasColumnName("ApplicabilityClassTypeId");
        this.Property(t => t.ManagedByUserId).HasColumnName("ManagedByUserId");
        this.Property(t => t.AssignedToUserId).HasColumnName("AssignedToUserId");
        this.Property(t => t.AreTasksAutoAccepted).HasColumnName("AreTasksAutoAccepted");
        this.Property(t => t.IncludeSourceEmissionLimits).HasColumnName("IncludeSourceEmissionLimits");

        this.HasRequired(t => t.ComplianceCategory)
            .WithMany()
            .HasForeignKey(t => t.ComplianceCategoryId);

        this.HasOptional(t => t.ManagedByUser)
            .WithMany()
            .HasForeignKey(t => t.ManagedByUserId);

        this.HasOptional(t => t.AssignedToUser)
            .WithMany()
            .HasForeignKey(t => t.AssignedToUserId);

        this.HasOptional(t => t.TaskFrequencyType)
            .WithMany()
            .HasForeignKey(t => t.TaskFrequencyTypeId);

        this.HasOptional(t => t.FrequencyType)
            .WithMany()
            .HasForeignKey(t => t.FrequencyTypeId);

        this.HasOptional(t => t.DueDateType)
            .WithMany()
            .HasForeignKey(t => t.DueDateTypeId);

        this.HasOptional(t => t.ApplicabilityClass)
            .WithMany()
            .HasForeignKey(t => t.ApplicabilityClassTypeId);

        this.HasRequired(t => t.ActionItemsToSource)
            .WithMany()
            .HasForeignKey(t => t.ActionItemId);

    }
}

ActionItemsToSource.cs:

public partial class ActionItemsToSource : BaseModel
{      
    public Guid ActionItemsToSourceId { get; set; }

    public Guid SourceId { get; set; }

    public Guid ActionItemId { get; set; }

    public Nullable<bool> ActiveStatus { get; set; }

    public Nullable<DateTime> StartCountdownDate { get; set; }

    public Nullable<DateTime> InitialTaskDueDate { get; set; }

    public Nullable<int> EstMonthlyAvgRuntime { get; set; }

    public string Notes { get; set; }

    public Nullable<Guid> AccountId { get; set; }

    public Source Source { get; set; }

    public ActionItem ActionItem { get; set; }
}

但是,我一直收到以下错误:

ActionItem_ActionItemsToSource_Source ::多重性在关系'ActionItem_ActionItemsToSource'中的角色'ActionItem_ActionItemsToSource_Source'中无效。由于“从属角色”是指关键属性,因此从属角色的多重性的上限必须为“1”。

1 个答案:

答案 0 :(得分:2)

ActionItempublic ActionItemsToSource ActionItemsToSource { get; set; }实际上应该是一个集合:

public ICollection<ActionItemsToSource> ActionItemsToSource { get; set; }

ActionItemMapthis.HasRequired(t => t.ActionItemsToSource).WithMany().HasForeignKey(t => t.ActionItemId);应更改为:

this.HasMany(t => t.ActionItemsToSource)
    .WithRequired(t => t.ActionItem)
    .HasForeignKey(t => t.ActionItemId);

ActionItemsToSourceMap中,必须同时调整映射(或删除映射,因为它定义了与第一个映射相同的关系,因此是多余的):

this.HasRequired(t => t.ActionItem)
    .WithMany(t => t.ActionItemsToSource)
    .HasForeignKey(d => d.ActionItemId);

或者,您可以完全删除导航属性和第一个映射,并保留第二个映射。