Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

60%
+1 −0
Q&A How to refer to the same class twice from one Entity Framework entity?

I have two classes, Contestant and Picture, with the following setup: public class Contestant { ... public int AvatarID { get; set; } [ForeignKey(nameof(AvatarID))] public ...

1 answer  ·  posted 1y ago by FrankLuke‭  ·  edited 1y ago by Alexei‭

#4: Nominated for promotion by user avatar Alexei‭ · 2022-10-16T06:39:09Z (over 1 year ago)
#3: Post edited by user avatar Alexei‭ · 2022-10-12T18:03:35Z (over 1 year ago)
added relevant tag
#2: Post edited by user avatar FrankLuke‭ · 2022-10-11T21:04:05Z (over 1 year ago)
  • How to refer to the same table twice from one Entity Framework entity?
  • How to refer to the same class twice from one Entity Framework entity?
#1: Initial revision by user avatar FrankLuke‭ · 2022-10-11T21:03:15Z (over 1 year ago)
How to refer to the same table twice from one Entity Framework entity?
I have two classes, Contestant and Picture, with the following setup:

    public class Contestant 
    {
        ...
        public int AvatarID { get; set; }

        [ForeignKey(nameof(AvatarID))]
        public virtual Picture? Avatar { get; set; }
	
        public virtual ICollection<Picture>? Pictures { get; set; }
        ...
    }

    public class Picture
    {
        ...
        public int ContestantID { get; set; }
	
        [ForeignKey(nameof(ContestantID))]
        public virtual Contestant? Contestant { get; set; }
        ...
    }


Each contestant instance has a set of pictures and a single picture from the set that serves as their avatar.

The original setup (without AvatarID or Avatar relationship) worked fine. After adding the Avatar field and relationship, I get a run-time error message when I try to load either the Contestant set or Picture set. It reads:


 > System.InvalidOperationException: 'Unable to determine the relationship represented by navigation 'Contestant.Avatar' of type 'Picture.' Either manually configure the relationship, or ignore this property using the '[NotMapper]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.' 

From my research, this happens when a class refers to another class twice. The answers I saw all involved the same class having both keys of the relationship. For example, a Project class might have multiple employees assigned, one as ProjectEngineer the other as AssignedDeveloper. For those, defining the [ForeignKey()] in the same file worked. I do not know what I need to do, here or possibly in the DbContext's OnModelCreating method.