0

I'm curious to know if a one to many relationship in entity framework creates another table? Or how does it work?

I have an event entity and a profile entity. When I create an event I will then add profile entities to a list/collection to the event properties RegisteredStudentIds to show who's registered.

But I don't want to create another table of profile entities, I just want to somehow link multiple profiles to that one event.

How would I create this and what would it do in the db in terms of tables created in addition to the two tables I described above (profile, event)?

Currently in my event entity I have a property called

public string RegisteredProfileIds { get; set; }

and in this string I have a string of profile ids that look like this

"12345,23234,34345,87678"

then I have to go through the process of splitting, adding to, removing from, etc, etc and then querying the id after I extract it from the string and it gets a little tedious! I'm just looking for a better solution.

chuckd
  • 13,460
  • 29
  • 152
  • 331
  • A many to many creates another table, you can easily have a relationship of List in Poco1. But only if you are setting up Code First. – djangojazz Jan 27 '17 at 22:08
  • Duplicate: http://stackoverflow.com/q/8927278/861716 – Gert Arnold Jan 27 '17 at 22:15
  • @Gert I think user1186050 has a subtle difference as he/she wants to know to know if he gets a many to many, not get that as stated by: 'But I don't want to create another table of profile entities, I just want to somehow link multiple profiles to that one event.' – djangojazz Jan 27 '17 at 22:19
  • @djangojazz I'm not sure. That's why I didn't hammer it down as duplicate. But the description is rather vague. I think the OP should tell what they think of the linked answer. – Gert Arnold Jan 27 '17 at 22:23
  • I'm implementing a test right now and will post soon – chuckd Jan 27 '17 at 22:27
  • Here is the problem I'm having. I'm following the example below and in my case I have a classentity and I'm adding students to that class! When I add the first student I see the list is null, so I create a new instance and then use .Add(Student). So far so good, but then I log in with a second student and add him to the class, but when I get to the RegisteredStudent property it's null again, it doesn't show the previously added student, so I have to create a new one and then add the new student. It's not saving the registered students properly! – chuckd Jan 27 '17 at 23:15
  • Don't add stories in comments. Add *code* to your question. – Gert Arnold Jan 28 '17 at 10:28

1 Answers1

0

If you just want to relate Two Entities where one can have many of another it is like this:

Parent Entity:

 public class Student
 {
    public int StudentID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public List<StudentTestScore> Scores { get; set; }
 }

Child Entity:

 public class StudentTestScore
 {
    public int StudentTestScoreID { get; set; }
    public int Score { get; set; }
 }

Update for insert into a possible context of EF:

var std = new Student { StudentID = 1, FirstName = "Brett", LastName = "X", Scores = new List<StudentTestScore>{ new Score { StudentTestScoreID = 1, Score = 100 }}}

  using (var context = new YOURContext())
  {
    context.Student.Add(std);
    context.SaveChanges();
  }
djangojazz
  • 14,131
  • 10
  • 56
  • 94
  • should the Scores property in Student be a List or a virtual ICollection in this case? – chuckd Jan 27 '17 at 22:17
  • I think EF when you are using a model first design(big screen that shows visuals of each table on an *.edmx file. Will create that collection type by default. You may override these manually after they are created by the T4 template(*.tt file). The key is you have a 'collection' type relating another object to it. – djangojazz Jan 27 '17 at 22:21
  • Please clarify something, when I use this example above and I add a 'StudentTestScore' to the 'Student' table I don't see any column created in the DB! Where is the link to that 'StudentTestScore' created?? – chuckd Jan 27 '17 at 22:36
  • This was a simple example to show relationship if you were to add this to the database you need a context. Hold up and let me update it. – djangojazz Jan 27 '17 at 22:39
  • Here is the problem I'm having. In my case I have a class and I'm adding students to that class! When I add the first student I see the list is null, so I create a new instance and then use .Add(Student). So far so good, but then I log in with a second student and add him to the class, but when I get to the RegisteredStudent property it's null again, it doesn't show the previously added student, so I have to create a new one and then add the new student. It's not saving the registered students properly! – chuckd Jan 27 '17 at 23:14
  • You should have a collection instantiated with 'new List<(Poco)>' and just add to it. If your collection is changing it could be scoping due to concurrency issues or other things. This is quite a bit far from your original question at this point. You need to update your question better as you are going in too many directions. You are mentioning 1. How a relationship works with one to many. 2. You are having a property that is a concatenation of many ids to then split somehow. 3. Another collection is getting reset. That is a lot of things for one question. – djangojazz Jan 27 '17 at 23:22