How to map two properties in one first code object with the same parent type

I was at this hour and tried many suggestions I found but no luck. I am using the first EF 5 code.

The situation is that I have an Employee class. Then I have another class that has two properties, both are of type Employee. I want these both to be foreign key constraints, but the requirement allows the same queries from the same users, so I can't just use them as keys. I don't care that the Worker has two collections to navigate, but in my work on a problem that seemed like a requirement. If that makes the problem easier, I can remove it.

I receive this message. System.Data.Entity.Edm.EdmAssociationEnd :: plurality is invalid in role "Employee_RequestsForEmployee_Target" in relation to "Employee_RequestsForEmployee". Since the properties of the dependent role are not key properties, the upper bound for the multiplicity of the dependent role must be "*".

I tried this using the Fluent API in the OnModelCreation method in the context;

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

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

      

Classes in conflict (I've removed some properties for clarity);

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

      

Any help would be much appreciated. Thanks in advance.

+3


source to share


1 answer


I've never figured out how to do this using data annotations, but using the Fluent API, I was able to do it. What I was missing was that I had to indicate in the HasMany () method that the relationship on the other side was, which I assumed was understood through data annotations and conventions.

This is caused in the DbContext override OnModelCreating (The WillCascadeOnDelete (false) is related to a different issue).

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

      



With classes:

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

      

+3


source







All Articles