I've got 2 entities, User and Material. User holds the Collection of the materials.
public class User
{
...
private ICollection<Material> materials;
public virtual ICollection<Material> Materials
{
get { return materials ?? (materials = new List<Material>()); }
set { materials = value; }
}
}
And material:
public class Material
{
...
public int? UserId { get; set; }
public virtual User User { get; set; }
...
}
When logged User selects Material, I assign that User to Material.User and save changes. Works fine, changes are saved in db. But, if I want to access materials with User.Materials, collection contains no elements. I have tried with simple project and there it works. With my complex project, it does not. What to do?
Help me please, bothering with problem for 8 hours already and still havent fix it.
EDIT: Well, that is so... lame.
My code was actually working BUT... the problem was, when view-ing user details, I retrieved User from database and created a COPY of it with a cunstrictor. I was missing something:
public User(User otherUser)
{
Id = otherUser.Id;
FirstName = otherUser.FirstName;
LastName = otherUser.LastName;
Shift = otherUser.Shift;
PassCode = otherUser.PassCode;
Type = otherUser.Type;
Materials = otherUser.Materials; // this line was missing
}
After adding this line, it works. Just Visual Studio is complaining about that (virtual member call in constructor). What to do about it?