none
When I try to do eager loading I get an error of null collection RRS feed

  • Question

  • I try to do the eager loading in this way:

    lstResultado = miContexto.Videos
                                .Include(v => v.Series.Select(s => s.Episodios))
                                .Include(v=> v.VideosVersiones)
                                .Include(v => v.Generos).ToList<Videos>();

    But I get this error: "The value can't be null. Name of the parameter: collection."

    If I use only Include(v => v.Series.Select(s => s.Episodios)) it works fine, but the problem is when I use Include(v=> v.VideosVersiones).

    My entities are:

    public partial class Videos
        {
            public Videos()
            {
                this.Series = new HashSet<Series>();
                this.VideosPersonas = new HashSet<VideosPersonas>();
                this.VideosVersiones = new HashSet<VideosVersiones>();
                this.Generos = new HashSet<Generos>();
            }
    
            public long IDVideo { get; set; }
            public string Titulo { get; set; }
            public string Version { get; set; }
            public Nullable<short> Duracion { get; set; }
            public Nullable<short> Anyo { get; set; }
            public bool Favorito { get; set; }
            public bool Pendiente { get; set; }
            public string Descripcion { get; set; }
            public Nullable<long> IDPortada { get; set; }
            public long IDTipo { get; set; }
    
            public virtual Ficheros Ficheros { get; set; }
            public virtual ICollection<Series> Series { get; set; }
            public virtual Tipos Tipos { get; set; }
            public virtual ICollection<VideosPersonas> VideosPersonas { get; set; }
            public virtual ICollection<VideosVersiones> VideosVersiones { get; set; }
            public virtual ICollection<Generos> Generos { get; set; }
        }
    
    
    
    public partial class VideosVersiones
        {
            public long IDVersion { get; set; }
            public long IDVideo { get; set; }
            public long IDEpisodio { get; set; }
    
            public virtual Episodios Episodios { get; set; }
            public virtual Versiones Versiones { get; set; }
            public virtual Videos Videos { get; set; }
        }
    
    
    
    public partial class Series
        {
            public Series()
            {
                this.Episodios = new HashSet<Episodios>();
            }
    
            public long IDSerie { get; set; }
            public long IDVideo { get; set; }
            public Nullable<short> AnyoInicio { get; set; }
            public Nullable<short> AnyoFin { get; set; }
            public Nullable<short> NumeroTemporadas { get; set; }
    
            public virtual ICollection<Episodios> Episodios { get; set; }
            public virtual Videos Videos { get; set; }
        }
    
    
    
    public partial class Episodios
        {
            public Episodios()
            {
                this.VideosVersiones = new HashSet<VideosVersiones>();
            }
    
            public long IDEpisodio { get; set; }
            public long IDSerie { get; set; }
            public byte Temporada { get; set; }
            public byte Episodio { get; set; }
            public string Titulo { get; set; }
            public Nullable<byte> Anyo { get; set; }
    
            public virtual Series Series { get; set; }
            public virtual ICollection<VideosVersiones> VideosVersiones { get; set; }
        }

    Thanks.

    Saturday, August 17, 2013 6:59 AM

Answers

  • Hello,

    Welcome toMSDN Support Forum.

    From your description, I notice the issue you are experiencing is that you get this error: "The value can't be null. Name of the parameter: collection." when you use Include(v=> v.VideosVersiones).

    If I have any misunderstood, please let me know.

    Since I do not know how you set up the table relationship, I assumed it myself with the code provided by you.

    I made a sample and please see the sample code below:

    Domain Class Code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace Code_First.Domain
    {
        public class VideosVersiones
        {
            public long IDVersion { get; set; }
            public long IDVideo { get; set; }
            public long IDEpisodio { get; set; }
            public virtual Episodios Episodios { get; set; }
            public virtual Videos Videos { get; set; }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace Code_First.Domain
    {
        public class Videos
        {
            public Videos()
            {
                this.Series = new HashSet<Series>();
                this.VideosVersiones = new HashSet<VideosVersiones>();
            }
            public long IDVideo { get; set; }
            public string Titulo { get; set; }
            public string Version { get; set; }
            public Nullable<short> Duracion { get; set; }
            public Nullable<short> Anyo { get; set; }
            public bool Favorito { get; set; }
            public bool Pendiente { get; set; }
            public string Descripcion { get; set; }
            public Nullable<long> IDPortada { get; set; }
            public long IDTipo { get; set; }
            public virtual ICollection<Series> Series { get; set; }
            public virtual ICollection<VideosVersiones> VideosVersiones { get; set; }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace Code_First.Domain
    {
        public class Series
        {
            public Series()
            {
                this.Episodios = new HashSet<Episodios>();
            }
            public long IDSerie { get; set; }
            public long IDVideo { get; set; }
            public Nullable<short> AnyoInicio { get; set; }
            public Nullable<short> AnyoFin { get; set; }
            public Nullable<short> NumeroTemporadas { get; set; }
            public virtual ICollection<Episodios> Episodios { get; set; }
            public virtual Videos Videos { get; set; }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace Code_First.Domain
    {
        public class Episodios
        {
            public Episodios()
            {
                this.VideosVersiones = new HashSet<VideosVersiones>();
            }
            public long IDEpisodio { get; set; }
            public long IDSerie { get; set; }
            public byte Temporada { get; set; }
            public byte Episodio { get; set; }
            public string Titulo { get; set; }
            public Nullable<byte> Anyo { get; set; }
            public virtual Series Series { get; set; }
            public virtual ICollection<VideosVersiones> VideosVersiones { get; set; }
        }
    }

    And the Map Class Code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity.ModelConfiguration;
    using System.ComponentModel.DataAnnotations.Schema;
    namespace Code_First.Domain
    {
        class VideosVersionesMap : EntityTypeConfiguration<VideosVersiones>
        {
            public VideosVersionesMap()
            {
                this.ToTable("VideosVersiones");
                this.HasKey(t => t.IDVersion);
                this.HasRequired(t => t.Videos)
                    .WithMany(v => v.VideosVersiones)
                    .HasForeignKey(v => v.IDVideo).WillCascadeOnDelete(false);
                this.HasRequired(t => t.Episodios)
                    .WithMany(v => v.VideosVersiones)
                    .HasForeignKey(v => v.IDEpisodio).WillCascadeOnDelete(false);
                this.Property(t => t.IDVersion).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); // 取消主键Identity
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.ModelConfiguration;
    using System.Linq;
    using System.Text;
    namespace Code_First.Domain
    {
        class VideosMap : EntityTypeConfiguration<Videos>
        {
            public VideosMap()
            {
                this.ToTable("Videos");
                this.HasKey(t => t.IDVideo);
                this.Property(t => t.IDVideo).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); // 取消主键Identity
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity.ModelConfiguration;
    using System.ComponentModel.DataAnnotations.Schema;
    namespace Code_First.Domain
    {
        class SeriesMap : EntityTypeConfiguration<Series>
        {
            public SeriesMap()
            {
                this.ToTable("Series");
                this.HasKey(t => t.IDSerie);
                this.Property(t => t.IDSerie).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); // 取消主键Identity
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity.ModelConfiguration;
    using System.ComponentModel.DataAnnotations.Schema;
    namespace Code_First.Domain
    {
        class EpisodiosMap : EntityTypeConfiguration<Episodios>
        {
            public EpisodiosMap()
            {
                this.ToTable("Episodios");
                this.HasKey(t => t.IDEpisodio);
                this.Property(t => t.IDEpisodio).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); // 取消主键Identity
            }
        }
    }

    The Context Class Code:

    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace Code_First.Domain
    {
        public class OPContext : DbContext
        {
            public DbSet<Videos> Videos { get; set; }
            public DbSet<VideosVersiones> VideosVersiones { get; set; }
            public DbSet<Series> Series { get; set; }
            public DbSet<Episodios> Episodios { get; set; }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Configurations.Add(new VideosMap());
                modelBuilder.Configurations.Add(new VideosVersionesMap());
                modelBuilder.Configurations.Add(new SeriesMap());
                modelBuilder.Configurations.Add(new EpisodiosMap());
            }
        }

    The Main Program Class Code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Code_First.Domain;
    using System.Data.Entity;
    namespace Code_First
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Create DB
                using (OPContext op = new OPContext())
                {
                    var lstResultado = op.Videos
                                 .Include(v => v.Series.Select(s => s.Episodios))
                                 .Include(v => v.VideosVersiones).ToList<Videos>();
                    foreach (var r in lstResultado)
                    {
                        Console.WriteLine("IDVideo:" + r.IDVideo);
                        foreach (var s in r.Series)
                        {
                            Console.WriteLine("IDSerie:" + s.IDSerie);
                            foreach (var e in s.Episodios)
                            {
                                Console.WriteLine("IDEpisodio:" + e.IDEpisodio);
                            }
                        }
                        foreach (var vv in r.VideosVersiones)
                        {
                            Console.WriteLine("IDVersion:" + vv.IDVersion);
                        }
                    }
                    op.SaveChanges();
                }
                Console.ReadLine();
            }
        }
    }

    Whether I use only Include(v => v.Series.Select(s => s.Episodios)) or use Include(v=> v.VideosVersiones).

    It works fine.

    If this does not work for you, please let me know.

    So if it is possible, could you please share your table map code so that we can help you better.

    I look forward to hearing from you.

    Best Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, August 19, 2013 9:44 AM
    Moderator

All replies

  • Hello,

    Welcome toMSDN Support Forum.

    From your description, I notice the issue you are experiencing is that you get this error: "The value can't be null. Name of the parameter: collection." when you use Include(v=> v.VideosVersiones).

    If I have any misunderstood, please let me know.

    Since I do not know how you set up the table relationship, I assumed it myself with the code provided by you.

    I made a sample and please see the sample code below:

    Domain Class Code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace Code_First.Domain
    {
        public class VideosVersiones
        {
            public long IDVersion { get; set; }
            public long IDVideo { get; set; }
            public long IDEpisodio { get; set; }
            public virtual Episodios Episodios { get; set; }
            public virtual Videos Videos { get; set; }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace Code_First.Domain
    {
        public class Videos
        {
            public Videos()
            {
                this.Series = new HashSet<Series>();
                this.VideosVersiones = new HashSet<VideosVersiones>();
            }
            public long IDVideo { get; set; }
            public string Titulo { get; set; }
            public string Version { get; set; }
            public Nullable<short> Duracion { get; set; }
            public Nullable<short> Anyo { get; set; }
            public bool Favorito { get; set; }
            public bool Pendiente { get; set; }
            public string Descripcion { get; set; }
            public Nullable<long> IDPortada { get; set; }
            public long IDTipo { get; set; }
            public virtual ICollection<Series> Series { get; set; }
            public virtual ICollection<VideosVersiones> VideosVersiones { get; set; }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace Code_First.Domain
    {
        public class Series
        {
            public Series()
            {
                this.Episodios = new HashSet<Episodios>();
            }
            public long IDSerie { get; set; }
            public long IDVideo { get; set; }
            public Nullable<short> AnyoInicio { get; set; }
            public Nullable<short> AnyoFin { get; set; }
            public Nullable<short> NumeroTemporadas { get; set; }
            public virtual ICollection<Episodios> Episodios { get; set; }
            public virtual Videos Videos { get; set; }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace Code_First.Domain
    {
        public class Episodios
        {
            public Episodios()
            {
                this.VideosVersiones = new HashSet<VideosVersiones>();
            }
            public long IDEpisodio { get; set; }
            public long IDSerie { get; set; }
            public byte Temporada { get; set; }
            public byte Episodio { get; set; }
            public string Titulo { get; set; }
            public Nullable<byte> Anyo { get; set; }
            public virtual Series Series { get; set; }
            public virtual ICollection<VideosVersiones> VideosVersiones { get; set; }
        }
    }

    And the Map Class Code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity.ModelConfiguration;
    using System.ComponentModel.DataAnnotations.Schema;
    namespace Code_First.Domain
    {
        class VideosVersionesMap : EntityTypeConfiguration<VideosVersiones>
        {
            public VideosVersionesMap()
            {
                this.ToTable("VideosVersiones");
                this.HasKey(t => t.IDVersion);
                this.HasRequired(t => t.Videos)
                    .WithMany(v => v.VideosVersiones)
                    .HasForeignKey(v => v.IDVideo).WillCascadeOnDelete(false);
                this.HasRequired(t => t.Episodios)
                    .WithMany(v => v.VideosVersiones)
                    .HasForeignKey(v => v.IDEpisodio).WillCascadeOnDelete(false);
                this.Property(t => t.IDVersion).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); // 取消主键Identity
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.ModelConfiguration;
    using System.Linq;
    using System.Text;
    namespace Code_First.Domain
    {
        class VideosMap : EntityTypeConfiguration<Videos>
        {
            public VideosMap()
            {
                this.ToTable("Videos");
                this.HasKey(t => t.IDVideo);
                this.Property(t => t.IDVideo).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); // 取消主键Identity
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity.ModelConfiguration;
    using System.ComponentModel.DataAnnotations.Schema;
    namespace Code_First.Domain
    {
        class SeriesMap : EntityTypeConfiguration<Series>
        {
            public SeriesMap()
            {
                this.ToTable("Series");
                this.HasKey(t => t.IDSerie);
                this.Property(t => t.IDSerie).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); // 取消主键Identity
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity.ModelConfiguration;
    using System.ComponentModel.DataAnnotations.Schema;
    namespace Code_First.Domain
    {
        class EpisodiosMap : EntityTypeConfiguration<Episodios>
        {
            public EpisodiosMap()
            {
                this.ToTable("Episodios");
                this.HasKey(t => t.IDEpisodio);
                this.Property(t => t.IDEpisodio).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); // 取消主键Identity
            }
        }
    }

    The Context Class Code:

    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace Code_First.Domain
    {
        public class OPContext : DbContext
        {
            public DbSet<Videos> Videos { get; set; }
            public DbSet<VideosVersiones> VideosVersiones { get; set; }
            public DbSet<Series> Series { get; set; }
            public DbSet<Episodios> Episodios { get; set; }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Configurations.Add(new VideosMap());
                modelBuilder.Configurations.Add(new VideosVersionesMap());
                modelBuilder.Configurations.Add(new SeriesMap());
                modelBuilder.Configurations.Add(new EpisodiosMap());
            }
        }

    The Main Program Class Code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Code_First.Domain;
    using System.Data.Entity;
    namespace Code_First
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Create DB
                using (OPContext op = new OPContext())
                {
                    var lstResultado = op.Videos
                                 .Include(v => v.Series.Select(s => s.Episodios))
                                 .Include(v => v.VideosVersiones).ToList<Videos>();
                    foreach (var r in lstResultado)
                    {
                        Console.WriteLine("IDVideo:" + r.IDVideo);
                        foreach (var s in r.Series)
                        {
                            Console.WriteLine("IDSerie:" + s.IDSerie);
                            foreach (var e in s.Episodios)
                            {
                                Console.WriteLine("IDEpisodio:" + e.IDEpisodio);
                            }
                        }
                        foreach (var vv in r.VideosVersiones)
                        {
                            Console.WriteLine("IDVersion:" + vv.IDVersion);
                        }
                    }
                    op.SaveChanges();
                }
                Console.ReadLine();
            }
        }
    }

    Whether I use only Include(v => v.Series.Select(s => s.Episodios)) or use Include(v=> v.VideosVersiones).

    It works fine.

    If this does not work for you, please let me know.

    So if it is possible, could you please share your table map code so that we can help you better.

    I look forward to hearing from you.

    Best Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, August 19, 2013 9:44 AM
    Moderator
  • Well, the problem was that I have used null value for IDEpisodio, but how this field is part of the primary key of the join table, this is a problem.

    So what I do is add a new field in the join table, IDJoinTable that is the primary key and now I can set null in the IDEpisodio field.

    I don't like a lot this solution but this let me to have only one join table that in my case save me work and I think is more efficient when I want to do a query.

    Thanks.

    Monday, August 19, 2013 9:48 AM