1

I have the following classes:

public class Category 
    {
        public int CategoryId { get; set; }
        public string Name { get; set; }
    }

public partial class CategoryMap : EntityTypeConfiguration<Category>
    {
        public CategoryMap()
        {
            this.HasKey(c => c.CategoryId);
            this.Property(c => c.Name).IsRequired().HasMaxLength(400);
        }
    }

public class MyObjectContext : DbContext
    {
        public MyObjectContext(string connectionStringName)
            : base(connectionStringName)
        {
        }

        public DbSet<Category> Categories { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new CategoryMap());
            base.OnModelCreating(modelBuilder);
        }
    }

Now when I run the following code I get an exception (GenericArguments[0], 'System.Int32', on 'System.Data.Entity.Internal.Linq.ReplacementDbQueryWrapper`1[TEntity]' violates the constraint of type 'TEntity')

DbDatabase.SetInitializer<MyObjectContext>(new DropCreateDatabaseIfModelChanges<MyObjectContext>());
                using (var context = new MyObjectContext("NopSqlConnection"))
                {
                    var query1 = from c in context.Categories
                                 select c.CategoryId;
                    var test1 = query1.ToList(); //works fine

                    var query2 = from c in context.Categories
                                 where query1.Contains(c.CategoryId)
                                 orderby c.Name descending
                                 select c;
                    var test2 = query2.ToList(); //throws the exception
                }

Any suggestions?

Andrei M
  • 3,429
  • 4
  • 28
  • 35

1 Answers1

0

It seems to work fine if you specify 'where' clause on query1

Andrei M
  • 3,429
  • 4
  • 28
  • 35