ragerory
ragerory

Reputation: 1378

ForeignKey Mapping not working on EntityFramework 5

ActionItem.ActionItemId is a FK in the ActionItemsToSource database. I have the following:

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

However, I keep getting the following error:

ActionItem_ActionItemsToSource_Source: : Multiplicity is not valid in Role 'ActionItem_ActionItemsToSource_Source' in relationship 'ActionItem_ActionItemsToSource'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'.

Upvotes: 0

Views: 66

Answers (1)

Slauma
Slauma

Reputation: 177133

In the ActionItem class public ActionItemsToSource ActionItemsToSource { get; set; } should actually be a collection:

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

And in the ActionItemMap class this.HasRequired(t => t.ActionItemsToSource).WithMany().HasForeignKey(t => t.ActionItemId); should be changed to:

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

In ActionItemsToSourceMap the mapping must then be adjusted as well (or removed because it defines the same relationship as the first mapping and therefore is redundant):

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

Alternatively you can remove both the navigation property and the first mapping altogether and keep the second mapping as you already have it.

Upvotes: 2

Related Questions