C#C
C#3y ago
Populus

✅ ForeignKey Entity Framework and Identity

I've got the following classes: "Account", "Post", "Tag" and "Image".
The "Post" class contains the following:
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid? PostId { get; set; }

        public List<Tag> Tags { get; set; }

        public List<Image> AttachedImages { get; set; }

        [ForeignKey("Account")]
        public Guid? OriginalPosterId { get; set; }
        public Account? OriginalPoster { get; set; }

        [ForeignKey("Account")]
        public List<Guid> PostParticipantsIds { get; set; }
        public virtual ICollection<Account> PostParticipants { get; set; }

        [ForeignKey("Account")]
        public List<Guid> UsersAttendingIds { get; set; }
        public virtual ICollection<Account> UsersAttending { get; set; }


I can't seem to quite understand how this type of mapping works. Why can't the Lists simply reference the correct AccountId? I see no reason there could not be different fields referencing the same "Id" for different purposes.

What am I doing wrong?
Was this page helpful?