13

I am using EF4 with WCF and POCO. I removed all virtual keyword in POCO entities.

I have Employee and Team entities and relationship between both is 1:N, means that one employee can be allocated only one team.

And I'd like to add new employee with existing team. Following code is in the client side.

private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            Team teamFromDb = ServiceProxy.GetService.GetTeamById(181);
            Employee newEmp = new Employee{ UserName="username"};
            newEmp.Team = teamFromDb;
            ServiceProxy.GetService.AddEmployee(newEmp);                
        }

Following code is in the server side (Dao layer)

public void AddEmployee(Employee emp)
        {
            ctx.Employees.AddObject(emp);
        }

        public Team GetTeamById(int teamId)
        {
            return ctx.Teams.Where(t => t.TeamId == teamId).FirstOrDefault();
        }

Problem is that I got "Collection was of a fixed size" Exception when I add teamFromDb instance to the newEmp.Team property in the client code.

Do I need to add some more code to fix?

In addition, What do I need to for Insert/Update/Delete job with POCO classes and WCF

Thanks in advance.

radu florescu
  • 4,315
  • 10
  • 60
  • 92
Ray
  • 4,038
  • 8
  • 36
  • 48
  • 11
    How did you manage to solve this problem? –  Feb 09 '11 at 13:33
  • 1
    For anyone coming across this issue still, make sure you don't initialize any collection properties with fixed-size data structures like empty Arrays. – Ryan Naccarato Apr 14 '21 at 22:42

3 Answers3

1

Did you tried to replace ICollection to FixupCollection for generated proxy entities(classes)? That may help if you are using WCF with entity framework POCOs

radu florescu
  • 4,315
  • 10
  • 60
  • 92
0

See this question for more info on this problem with the template:

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/71c04d6a-c8cf-4ef1-be7f-249bf8dc9c63/

Vaccano
  • 78,325
  • 149
  • 468
  • 850
0

This is a super old question. But I ran into it today. Just want to share what I solved it.

For me , the cause is I have something like this

Company company = _dbContext.Company.First()
company.Employees = company.Employees.Where(e => e.age > 30);

And the 2nd line messed up my Context because there are FK restrictions for Company and Employee.

To fix, just simply use a local variable

var tmpList =  company.Employees.Where(e => e.age > 30);
sean717
  • 11,759
  • 20
  • 66
  • 90