I use CodeFirst . It is necessary to make several links between 2 tables.

Classes are like this:

 public class User { public int Id { get; set; } public string Name { get; set; } public ICollection<Topic> Topics { get; set; } public User() { Topics = new List<Topic>(); } } public class Topic { public int Id { get; set; } public string Title { get; set; } public int TopicStarterId { get; set; } public User TopicStarterUserId { get; set; } public int? TopicModerId { get; set; } public User TopicModerUserId { get; set; } } 

Question 1: How to correctly name properties in this case? Do I correctly name TopicStarterUserId , TopicModerUserId ? Tell me, please, the naming rule for one-to-many binding.

Question 2: Which Fluent API attributes should be used if the naming does not comply with the rules? For example, I want to call TopicModerId just ModerId .

Question 3: In this example, it turns out that in order to make a second link (TopicModerId), you need to create an additional TopicModer table and link through it? If so, what will this table look like and how does it communicate with User and Topic ?

  • And according to the rules for the design of questions on so: it is accepted to ask the rules of good tone one question - one topic. - AK
  • one
    @AK thanks. I thought because Questions refer to the same topic, you can ask all together. Tell me, is it better to separate and create separate questions or leave as is? - tretetex
  • Leave as is. Indeed, very close questions on one topic. - AK
  • one
    Explain the logic of the topic moderator (question 3), is not entirely clear. A topic may not have a moderator, it can be moderated by someone. If repeatedly moderated - you need to save the whole story who moderated? It seems to me that it is difficult to guess exactly what you want to implement. - AK
  • Moderator can either be or not, always alone, the story is not needed. Based on your answer below, I realized that I would not need an additional table of moderators, since it is enough to specify the attributes to make 2 one-to-many links. - tretetex

1 answer 1

Make the necessary connections with attributes:

 public class Topic { public int ID { get; set; } public string Title { get; set; } public int AuthorID { get; set; } [ForeignKey("AuthorID")] public virtual User Author { get; set; } public int? ModeratorID { get; set; } [ForeignKey("ModeratorID")] public virtual User Moderator { get; set; } } 

Property names need not start with Topic *

Instead of Starter it is better to use the term Author.

 public class User { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ID { get; set; } public string Name { get; set; } [ForeignKey("AuthorID")] public virtual ICollection<Topic> TopicsAsAuthor { get; set; } [ForeignKey("ModeratorID")] public virtual ICollection<Topic> TopicsAsModerator { get; set; } } 
  • And if I want to get all the topics of one user, the author of which he is, will EF return them when accessing user1.Topics within the context? How does he know by what key to identify the author (AuthorID or ModeratorID)? - tretetex
  • @Skrim added an answer. - AK
  • @AK thank you very much, there are no more questions. - tretetex