日期:2014-05-20  浏览次数:20983 次

求助! EF4.1 查询报错!
我先把问题说一下:

Schedule 是 Tasks 的一个外键, tasks.ScheduleId = Schedule.ScheduleId

EF 4.1 中的基本的定义代码如下:  

C# code


public class Tasks
    {

        public int TaskId { get; set; }

       public string TaskName { get; set; }

        public DateTime StartDate { get; set; }

        public string StartTime { get; set; }

        public string EndTime { get; set; }

        public DateTime EndDate { get; set; }

        public virtual Schedule Schedule { get; set; }

        public virtual ICollection<Schedule> Schedules { get; set; }
        public virtual ICollection<Tag> Tags { get; set; }
        public virtual UserInfo UserInfo { get; set; }


    }

    public class Schedule
    {
        public int ScheduleId { get; set; }

        public int TaskId { get; set; }

        public DateTime LastExecutionTime { get; set; }

        public DateTime NextExecutionTime { get; set; }

        public virtual Tasks Tasks { get; set; }

    }

  //Tasks
            modelBuilder.Entity<Tasks>().HasKey(o => o.TaskId).ToTable("Tasks");
            modelBuilder.Entity<Tasks>().Property(o => o.TaskId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
           
            modelBuilder.Entity<Tasks>().Property(o => o.StartDate).IsOptional().HasColumnType("datetime2");
            modelBuilder.Entity<Tasks>().Property(o => o.StartTime).IsRequired();
            modelBuilder.Entity<Tasks>().Property(o => o.EndTime).IsRequired();
            modelBuilder.Entity<Tasks>().Property(o => o.EndDate).IsOptional().HasColumnType("datetime2");
            modelBuilder.Entity<Tasks>().Property(o => o.Desc).IsOptional().HasMaxLength(512);

            modelBuilder.Entity<Tasks>().Ignore(o => o.Schedule);  

          // Schedule

            modelBuilder.Entity<Schedule>().HasKey(o => o.ScheduleId).ToTable("Schedule");
            modelBuilder.Entity<Schedule>().Property(o => o.ScheduleId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<Schedule>().Property(o => o.ScheduleTypeId).IsRequired();
            modelBuilder.Entity<Schedule>().Property(o => o.LastExecutionTime).IsOptional().HasColumnType("datetime2");
            modelBuilder.Entity<Schedule>().Property(o => o.NextExecutionTime).IsOptional().HasColumnType("datetime2");
     
            modelBuilder.Entity<Schedule>().HasRequired(o => o.Tasks).WithMany(o => o.Schedules).HasForeignKey(f => f.TaskId);


       




现有业务需要,我要查一些 Task 
条件是 task.userid == 1 并且 Task . Schedule.NextExecutionTime 小于指定的时间

查询的方法是: GetMany  
基本的代码在下面。

C# code



 public virtual IEnumerable<T> GetMany(
          Expression<Func<T, bool>> filter = null,
          Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
          string includeProperties = "")
        {
            IQueryable<T> query = dbSet;

            if (filter != null)
            {
                query = query.Where(filter);
            }

            foreach (var includeProperty in includeProperties.Split
                (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                query = query.Include(includeProperty);
            }

            if (orderBy != null)
            {
                return orderBy(query).ToList();
            }
            else
            {
                return query.ToList();
            }
        }

       return taskRepository.GetMany(
                                           filter: k => k.UserId == userId || k.Schedule.NextExecutionTime < nextExecutionTime,
                                           orderBy: o => o.OrderBy(k => k.TaskId),
                                           includeProperties: "Schedules,Tags");