1

我有以下课程:

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);
        }
    }

现在,当我运行以下代码时,我得到一个异常(GenericArguments [0],'System.Int32','System.Data.Entity.Internal.Linq.ReplacementDbQueryWrapper`1[TEntity]'违反了'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
                }

有什么建议么?

4

1 回答 1

0

如果您在 query1 上指定 'where' 子句,它似乎工作正常

于 2011-01-03T09:09:20.297 回答