0

我有一个 ProductRequests 表。它与 ProductRequestDepartments 具有一对一的关系。哪个工作正常。我想链接 ProductRequestDetails (它将具有 ProductRequests 的实际产品(1 个或多个)。

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

如何在 Fluent API 中连接它。到目前为止我试过

    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 工作正常 (1 : 1) ProductRequests -> ProductRequestDetail 不工作 (1: N)

我越来越

在模型生成期间检测到一个或多个验证错误:“WP__ProductRequests_ProductRequestDetails_Source: : Multiplicity is not valid in Role 'WP__ProductRequests_ProductRequestDetails_Source' in relationship 'WP__ProductRequests_ProductRequestDetails'. 因为从属角色指的是关键属性,所以从属角色的多重性的上限必须为“1”。

4

1 回答 1

0

我相信你正在寻找

modelBuilder.Entity<WP__ProductRequestDetails>()
  .HasRequired(productRequestDetails => productRequestDetails.ProductRequest)
  .WithMany(productRequest => productRequest.ProductRequestDetails)
  .HasForeignKey(productRequestDetails => productRequestDetails.RequestId);
于 2014-08-15T18:53:08.697 回答