Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How can we have a one-to-many relationship when the primary key of all the models named Id?

Example:

public class Organization {
    public long Id { get; set; }
    public List<Teacher> Teachers { get; set; }
}

public class Teacher {
    public long Id { get; set; }
    public long ??? { get; set; } // what do we call OrganizationId?
    public Organization Organization { get; set; }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
385 views
Welcome To Ask or Share your Answers For Others

1 Answer

The name of the variable with ??? should be called OrganizationId. I would create a separate class with the configuration for the model Teacher.

For example:

class TeacherConfiguration : IEntityTypeConfiguration<Teacher>
{
    public void Configure(EntityTypeBuilder<BoardCard> builder)
    {
        builder.HasKey(bc => bc.Id);
        builder.HasOne(bc => bc.Organization).WithMany(c => c.Teachers).HasForeignKey(bc => bc.OrganizationId).OnDelete(DeleteBehavior.Cascade).IsRequired();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...