0

I have a ProductRequests table. It has a one to one relationship to ProductRequestDepartments. Which works correctly. I want to link ProductRequestDetails (which will have the actual Products (1 or more) of the ProductRequests.

public partial class WP__ProductRequests
{
    [Key]
    public int RequestId { get; set; }

    [Required]
    [StringLength(30)]
    public string FromLocation { get; set; }

    [Required]
    public int ToDepartmentId { get; set; }

    [StringLength(4000)]
    public string Reason { get; set; }

    [Required]
    [StringLength(50)]
    public string CreatedBy { get; set; }

    public DateTime CreatedDate { get; set; }

    [StringLength(50)]
    public string CompletedBy { get; set; }

    public DateTime? CompletedDate { get; set; }

    [Required]
    [StringLength(1)]
    public string Status { get; set; }

    public ICollection<WP__ProductRequestDetails> ProductRequestDetails { get; set; }

    public ICollection<WP__ProductRequestDepartments> ProductRequestDepartments { get; set; }
}

public partial class WP__ProductRequestDetails
{
    [Key]
    public int RequestDetailsId { get; set; }

    [Required]
    public int RequestId { get; set; }

    [StringLength(20)]
    public string ItemCode { get; set; }

    [StringLength(100)]
    public string ItemName { get; set; }

    public int? Quantity { get; set; }

    [Required]
    [StringLength(1)]
    public string Approved { get; set; }

    public WP__ProductRequests ProductRequest { get; set; }
}

public partial class WP_ProductRequestDepartments
{
    [Key]
    public int ID { get; set; }

    [StringLength(100)]
    public string Department { get; set; }

    public int? ApprovalManager { get; set; }

    [StringLength(60)]
    public string Reason { get; set; }

    [StringLength(25)]
    public string GeneralLedger { get; set; }
}

How do I wire this up in the Fluent API. So far I tried

    public virtual DbSet<WP__ProductRequestDepartments> WP__ProductRequestDepartments { get; set; }
    public virtual DbSet<WP__ProductRequestDetails> WP__ProductRequestDetails { get; set; }
    public virtual DbSet<WP__ProductRequests> WP__ProductRequests { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<WP__ProductRequestDetails>()
            .Property(e => e.Approved)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<WP__ProductRequests>()
            .Property(e => e.Status)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<WP__ProductRequests>()
            .HasRequired(a => a.ProductRequestDepartments)
            .WithMany()
            .HasForeignKey(a => a.ToDepartmentId);

        //??
        modelBuilder.Entity<WP__ProductRequests>()
            .HasRequired(a => a.ProductRequestDetails)
            .WithMany()
            .HasForeignKey(a => a.RequestId);

    }

ProductRequests -> ProductRequestDepartments works correctly (1 : 1) ProductRequests -> ProductRequestDetail does NOT work (1 : N)

I'm getting

One or more validation errors were detected during model generation:" WP__ProductRequests_ProductRequestDetails_Source: : Multiplicity is not valid in Role 'WP__ProductRequests_ProductRequestDetails_Source' in relationship 'WP__ProductRequests_ProductRequestDetails'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150

1 Answers1

0

I believe you are looking for

modelBuilder.Entity<WP__ProductRequestDetails>()
  .HasRequired(productRequestDetails => productRequestDetails.ProductRequest)
  .WithMany(productRequest => productRequest.ProductRequestDetails)
  .HasForeignKey(productRequestDetails => productRequestDetails.RequestId);
Erik Philips
  • 53,428
  • 11
  • 128
  • 150