I have an User class that have the following fields:
public int Id { get; set; }
public ICollection<Event> AdminEvents { get; set; }
public ICollection<Event> SubscribedEvents { get; set; }
And my Event class looks like:
public int Id { get; set; }
public ICollection<User> Subscribed { get; set; }
public User Admin { get; set; }
public int AdminId { get; set; }
OnModelCreating in my Context:
modelBuilder.Entity<User>().HasMany(u => u.SubscribedEvents).WithMany(u => u.Subscribed);
modelBuilder.Entity<User>().HasMany(u => u.AdminEvents).WithRequired(u => u.Admin).WillCascadeOnDelete(false);
- The point here is that User can create an Event (
Event.Admin && Admin.Id) and can be subscribed to others events (User.SubscribedEvents && Event.Subscribed) -
When i try to access
User.SubcribedEvents.ToList()it throws the
following error:Value cannot be null.Parameter name: source
But inserting works, and the value goes into the right table:
**dbo.UserEvents**. - What’s wrong with my entities design?. If you need more information
to trace the trouble please ask. Thanks!
Your navigation and collection properties (at least) should be virtual so they can be lazily initialized.