I have this situation: (simplified for ease of reading)
2 Business entities with bi-directional many-to-many relationship.
Code:
namespace TestProject
{
public class Program
{
private Decimal _programId;
private String _name;
private IList<Project> _projects = new List<Project>();
public virtual Decimal ProgramId { get; set; }
public virtual String Name { get; set; }
public virtual IList<Project> Projects
{
get
{
return new List<Project>(_projects).AsReadOnly();
}
protected set
{
_projects = value;
}
}
}
public class Project
{
private Decimal _projectId;
private String _name;
private IList<Program> _program = new List<Program>();
public virtual Decimal ProjectId { get; set; }
public virtual String Name { get; set; }
public virtual IList<Program> Programs
{
get
{
return new List<Program>(_programs).AsReadOnly();
}
protected set
{
_programs = value;
}
}
}
}
with these mappings:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Program" table="PROGRAM">
<id name="ProgramId" type="Decimal">
<column name="PROGRAM_ID"/>
<generator class="sequence">
<param name="sequence">PROGRAM_SQ</param>
</generator>
</id>
<property name="Name" type="String">
<column name="NAME" length="50" not-null="true"/>
</property>
<idbag name="Projects" inverse="true" table="PROGRAM_PROJECT">
<collection-id column="PROGRAM_PROJECT_ID" type="Decimal">
<generator class="sequence">
<param name="sequence">PROGRAM_PROJECT_SQ</param>
</generator>
</collection-id>
<key>
<column name="PROGRAM_ID" not-null="true"/>
</key>
<many-to-many class="Project" column="PROJECT_ID"/>
</idbag>
</class>
<class name="Project" table="PROJECT">
<id name="ProjectId" type="Decimal">
<column name="PROJECT_ID"/>
<generator class="sequence">
<param name="sequence">PROJECT_SQ</param>
</generator>
</id>
<property name="Name" type="String">
<column name="NAME" length="100" not-null="true"/>
</property>
<idbag name="Programs" inverse="true" table="PROGRAM_PROJECT">
<collection-id column="PROGRAM_PROJECT_ID" type="Decimal">
<generator class="sequence">
<param name="sequence">PROGRAM_PROJECT_SQ</param>
</generator>
</collection-id>
<key>
<column name="PROJECT_ID" not-null="true"/>
</key>
<many-to-many class="Program" column="PROGRAM_ID"/>
</idbag>
</class>
</hibernate-mapping>
If you notice, I have set inverse="true" on both sides of the relationship for now - I will make one side responsible after I figure out this issue.
I have populated the database with a few Programs, a few Projects, and populated the intermediary PROGRAM_PROJECT table so they have ties back to each other.
NOTE: This relationship is LAZY at both ends also. (I have tried putting lazy="true" in each idbad and still get the same exception)
For this example lets say I have some Projects named Test 1, Test 2, Test 3, Testing 1, Testing 2, Testing 3.
Code:
ICriteria criteria = session.CreateCriteria(typeof(Project)).Add(Expression.Like("Name", "%Testing%"));
IList<Project> Projects = criteria.List<Project>() as List<Project>;
This gives me Count = 3 and works fine.
The problem begins when I execute the next segment of code to get a list of all Programs (to populate a dropdown to use as a filter for listing projects by program).
Note I use criteria here as I will filter out inactive items using a status field later on, but for now i'll leave that out.
Code:
ICriteria program_criteria = session.CreateCriteria(typeof(Program));
IList<Program> Programs = program_criteria.List<Program>() as List<Program>;
This throws the following exception:
Code:
Server Error in '/' Application.
--------------------------------------------------------------------------------
Collection was modified; enumeration operation may not execute.
I can't for the life of me figure out why this would happen. Any help would be GREATLY appreciated.
Thanks in advance.