0

I want that UserMessage entity point with ReciverIdFK to SharedManWoman entity and with SenderIdFK to another SharedManWoman entity

public class SharedManWoman
{
    public List<UserMessage> UserMessages { get; set; }
}

public class UserMessage
{      
        [Key]
        public long Id { get; set; }

        [ForeignKey("UserMessages")]
        public long SenderIdFK { get; set; }
        [InverseProperty("UserMessages")]
        public virtual SharedManWoman UserMessages { get; set; } 

        [ForeignKey("UserMessages")]
        public long ReciverIdFK { get; set; }
        [InverseProperty("UserMessages")]
        public virtual SharedManWoman UserMessages { get; set; } 
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Link
  • 57
  • 4

1 Answers1

1

Well, first of all, you can't have two properties with the same name.

But, using the following fluent api on at least one of the properties should do the job:

modelBuilder.Entity<UserMessage>()
   .HasOptional(t => t.UserMessages)
   .WithMany()
   .WillCascadeOnDelete(false);

EDIT

If you want to do it with annotations, the following should do the job. Note that your SharedManWoman object needs an Id. Also, as it stands, the SharedManWoman's pretty much useless since it doesn't have any properties of its own.

public class SharedManWoman {
   public long Id { get; set; }

   [InverseProperty("Sender")]
   public ICollection<UserMessage> SenderMessages { get; set; }

   [InverseProperty("Receiver")]
   public ICollection<UserMessage> ReceiverMessages { get; set; }
}

public class UserMessage {

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

   // Note that these are NULLABLE
   public long? SenderIdFK { get; set; }
   public long? ReceiverIdFK { get; set; }


   [ForeignKey("SenderIdFK")]
   public virtual SharedManWoman Sender { get; set; }

   [ForeignKey("ReceiverIdFK")]
   public virtual SharedManWoman Receiver { get; set; }
}

More Info Here

Community
  • 1
  • 1
Steve
  • 181
  • 2
  • 7