2

Tool: Visual Studio 2010
Language: C#

I have just started learning Entity Framework,I'm stuck in a problem,whenver I used Code#1 it works fine but whenever I use CODE#2,I get error (posted below)

Title: InvalidOperationException was unhandled by user code
Error Message "The EntityCollection has already been initialized. The InitializeRelatedCollection method should only be called to initialize a new EntityCollection during deserialization of an object graph."

//SchoolModel.Designer.cs
public EntityCollection<Course> Courses
{
    get
    {  //Blah blah code }
    set
    {
        if ((value != null))
        {//Below statement is pointed by Visual Studio as Exception Thrower
            ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<Course>("SchoolModel.CourseInstructor", "Course", value);
        }
    }
}

CODE# 1:

List<string> list = new List<string>();
var prs = new Person();
using (var myEntity = new SchoolEntities())
{
    var result = myEntity.People;
    foreach (var ppl in result)
    {
        list.Add(ppl.PersonID+","+ppl.FirstMidName);
    }
}

CODE# 2:

List<string> list = new List<string>();
List<Person> prsList = new List<Person>();//when using this list,problem started
var prs = new Person();
using (var myEntity = new SchoolEntities())
{
    var result = myEntity.People;
    foreach (var ppl in result)
    {
        list.Add(ppl.PersonID+","+ppl.FirstMidName);

        //New code which raised exceptions
        prs.PersonID = ppl.PersonID;
        prs.FirstMidName = ppl.FirstMidName;
        prs.LastName = ppl.LastName;
        prs.Courses = ppl.Courses;
        prsList.Add(prs);
        //New code end
    }
}

Database Diagram:
Database Diagram from tutorial

Entity Diagram:
enter image description here

P.S:

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
DayTimeCoder
  • 4,294
  • 5
  • 38
  • 61

2 Answers2

3

You should not set an EntityCollection, as you do in prs.Courses = ppl.Courses. The collection has already been initialized (as per exception). You only modify it by Adding Course instances to it.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
  • When I commented out //prs.Courses = ppl.Courses; it started working ,Courses is a Navigation Property ,If I want to pick up Courses info of people,then what should I do ? – DayTimeCoder Mar 19 '12 at 19:23
  • 1
    You already picked it up :). I think `ppl.Courses` has courses in it. You can loop through it and add copies of the course to `prs.Courses`. – Gert Arnold Mar 19 '12 at 19:30
  • Well I just applied foreach loop to it,+1 for that also – DayTimeCoder Mar 19 '12 at 19:45
  • Hey its off the topic but can you tell me of any EF tute ,where "how to Query thru EF model by using code is discussed" – DayTimeCoder Mar 19 '12 at 19:46
  • 1
    I don't know any. My tutorial is MSDN + Stack Overflow + the odd web page that I happen to find. [This](http://www.ladislavmrnka.com/) is a nice blog. This guy is just brilliant! (And he's all over the place here...) – Gert Arnold Mar 19 '12 at 19:50
  • @GetArnold thanks ,make sure to check my other possible questions If I stuck again most probably :), +1 again – DayTimeCoder Mar 19 '12 at 19:52
0

Can you try by moving the initialization of prs inside of the foreach loop.

VIJAY
  • 849
  • 11
  • 22
  • I just did that but same error and in my opinion this won't have any affect,well I would love to reuse "prs" object its kinda temporary object – DayTimeCoder Mar 19 '12 at 08:04