【问题标题】:Entity Framework CTP5, code-first. Nested query error实体框架 CTP5,代码优先。嵌套查询错误
【发布时间】:2011-01-03 17:09:20
【问题描述】:

我有以下课程:

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', on '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
                }

有什么建议吗?

【问题讨论】:

    标签: asp.net entity-framework-4 code-first ef4-code-only


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 2011-08-19
      • 1970-01-01
      • 1970-01-01
      • 2013-01-02
      • 1970-01-01
      相关资源
      最近更新 更多