0

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?

MaTTo
  • 2,326
  • 5
  • 20
  • 24
  • You have removed too much code. There is nothing about your problem. – t3chb0t Dec 07 '14 at 20:56
  • There's nothing else to show. When I select material, I call a method, which set material's user property to currently logged in user, like so: `public void SetBoxAsUnused(Material material, User user) { material.User = user context.SaveChanges(); }` – MaTTo Dec 07 '14 at 21:06
  • "Virtual member call in constructor" is just a warning. If `Materials` is overridden in a subclass (e.g. a proxy!) you may get [unexpected results](http://stackoverflow.com/a/119543/861716). The solution is to assign `otherUser.Materials` to `materials` (the member variable, which, by the way, you may want to name `_materials` according to common naming conventions). – Gert Arnold Dec 07 '14 at 21:58

1 Answers1

0

Your problem are here:

    private ICollection<Material> materials;

    public virtual ICollection<Material> Materials
    {
        get { return materials ?? (materials = new List<Material>()); }
        set { materials = value; }
    }

Entity Framework navigation property cannot be property with backing field. It can be only autoproperty. So you should use this code instead:

public class User
{
...

        public virtual ICollection<Material> Materials
        {
            get;
            set;
        }
}
Kirill Bestemyanov
  • 11,946
  • 2
  • 24
  • 38
  • I get same result with autoproperty. – MaTTo Dec 07 '14 at 21:04
  • did you use lazy loading? Is dbcontext open when you try to look at navigation property? Show us code when you get data from database and code when you get null in navigation property. – Kirill Bestemyanov Dec 07 '14 at 21:07
  • I am using lazy loading. It works for every 1:M relationship in my project, problem exitsts just between those two, where I am later setting a navigation property. Anyway, to retreive user: `return context.Users.Find(id);` Works just fine, just collection is empty. Here I get null exception: `//MessageBox.Show(User.Materials.Count.ToString());`˙ Also, I bound this collection to listbox but is offcourse empty. – MaTTo Dec 07 '14 at 21:12
  • if you want us to help you should show in your question this code with details (so we can see where is your dbcontext initialized and so on). I suppose, that you have problem of disposed context in this scenario. – Kirill Bestemyanov Dec 07 '14 at 21:15
  • I added some more code. UserRepository is basicly the same, jsut returning User entity. – MaTTo Dec 07 '14 at 21:21