3

我已经在这里待了几个小时,并尝试了很多我发现的建议,但没有运气。我首先使用代码 EF 5。

情况是我有一个 Employee 类。然后我有另一个类,上面有两个属性,都是 Employee 类型。我希望这两个都是外键约束,但要求允许来自相同用户的许多相同请求,所以我不能只将它们用作键。我并不真正关心员工拥有两个导航集合,但在我解决似乎需要的问题时。如果它简化了问题,我可以删除这些。

我得到这个消息。System.Data.Entity.Edm.EdmAssociationEnd: : 多重性在关系“Employee_RequestsForEmployee”中的角色“Employee_RequestsForEmployee_Target”中无效。因为从属角色属性不是关键属性,所以从属角色的多重性的上限必须是“*”。

我已经在上下文的 OnModelCreation 方法中使用 Fluent API 进行了尝试;

modelBuilder.Entity() .HasRequired(u => u.ForEmployee) .WithMany() .HasForeignKey(u => u.ForEmployeeId);

        modelBuilder.Entity<RevenueTransferRequest>()
                    .HasRequired(u => u.FromEmployee)
                    .WithMany()
                    .HasForeignKey(u => u.FromEmployeeId);

有冲突的类是(为了清楚起见,我删除了一些属性);

    public class Employee : IEmployee
    {
        [Key]
        public string Id { get; set; }

        [InverseProperty("ForEmployee")]
        public ICollection<RevenueTransferRequest> RequestsForEmployee { get; set; }

        [InverseProperty("FromEmployee")]
        public ICollection<RevenueTransferRequest> RequestsFromEmployee { get; set; }
    }

 public class RevenueTransferRequest : IRevenueTransferRequest
    {
        [Key]
        public Guid Id { get; set; }

        [Required]
        [ForeignKey("ForEmployee")]
        public String ForEmployeeId { get; set; }

        [InverseProperty("RequestsForEmployee")]
        public Employee ForEmployee { get; set; }

        [Required]
        [ForeignKey("FromEmployee")]
        public String FromEmployeeId { get; set; }

        [InverseProperty("RequestsFromEmployee")]
        public Employee FromEmployee { get; set; }
    }

任何帮助将非常感激。提前致谢。

4

1 回答 1

3

我从来没有弄清楚如何使用数据注释来做到这一点,但使用 Fluent API 我能够做到这一点。我缺少的是我必须在 HasMany() 方法中指定另一边的关系是什么,我认为这是通过数据注释和约定来理解的。

这在 DbContext OnModelCreating 覆盖中调用(WillCascadeOnDelete(false) 与另一个问题有关)。

    modelBuilder.Entity<RevenueTransferRequest>()
                .HasRequired(e => e.FromEmployee)
                .WithMany(x=>x.RequestsFromEmployee)
                .WillCascadeOnDelete(false);

    modelBuilder.Entity<RevenueTransferRequest>()
              .HasRequired(e => e.ForEmployee)
              .WithMany(x => x.RequestsForEmployee)
              .WillCascadeOnDelete(false);

随着课程:

[Key]
        public String Id { get; set; }

        public String BusinessUnitLeaderId { get; set; }

        public Employee BusinessUnitLeader { get; set; }

        [Required]
        [MaxLength(150)]
        public String DisplayName { get; set; }

        public ICollection<Project> BusinessUnitLeaderProjects { get; set; }

        public ICollection<RevenueTransferRequest> RequestsForEmployee { get; set; }

        public ICollection<RevenueTransferRequest> RequestsFromEmployee { get; set; } 

public class RevenueTransferRequest
    {
        [Key]
        public Guid Id { get; set; }

        [Required]
        public String ForEmployeeId { get; set; }

        public Employee ForEmployee { get; set; }

        [Required]
        public String FromEmployeeId { get; set; }

        public Employee FromEmployee { get; set; }

        [Required]
        public String ProjectId { get; set; }

        public Project Project { get; set; }

        [Required]
        public Double? TransferAmount { get; set; }

        public int WorkflowState { get; set; }
    }
于 2013-02-15T21:36:14.060 回答