1

有人可以告诉我为什么在使用 select 语句后我的 where 语句中没有属性,例如

db.Select(x => x.Lft).Where(x => x.DepartmentId == id); 
// missing properties in the where clause

你能帮我纠正我的代码来实现它吗请给我一个例子来说明如何实现这个谢谢

课程:

public class Department
{
    public Department()
    {
        Products = new List<Product>();
    }

    public long DepartmentId { get; set; }

    [Required(ErrorMessage="Please enter a name for the departments.")]
    [DataType(DataType.Text)]
    public string Name { get; set; }

    [DataType(DataType.Text)]
    [Required(ErrorMessage = "Please enter a valid url for the department.")]
    public string Url { get; set; }

    public int Lft { get; set; }
    public int Rgt { get; set; }
    public bool MenuItem { get; set; }

    public virtual ICollection<Product> Products { get; set; }
}

我的 DataContext 类

internal class DepartmentsTypeConfiguration : EntityTypeConfiguration<Department>
{
    public DepartmentsTypeConfiguration()
    {
        Property(department => department.DepartmentId)
            .HasColumnName("DepartmentId")
            .HasDatabaseGeneratedOption(databaseGeneratedOption: DatabaseGeneratedOption.Identity);

        Property(department => department.Name)
            .HasColumnName("Name")
            .IsRequired();

        HasKey(key => key.DepartmentId)
            .HasMany(x => x.Products)
            .WithRequired(x => x.Department)
            .WillCascadeOnDelete(true);            
    }
}


public class LeapFrogDataContext : DbContext
{
    public DbSet<Department> Departments { get; set; }
    public DbSet<Product> Products { get; set; }
    public DbSet<ProductSpecification> ProductSpecifications {get; set;}
    public DbSet<Specification> Specifications { get; set; }
    /**/
    static LeapFrogDataContext()
        //: base("name=LeapFrogDataConnection")
    {
        //Database.SetInitializer(new LeapFrogInitializer());
        //Database.SetInitializer(new DropCreateDatabaseIfModelChanges<LeapFrogDataContext>());
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new DepartmentsTypeConfiguration());
        modelBuilder.Configurations.Add(new ProductsTypeConfiguration());
        modelBuilder.Configurations.Add(new SpecificationsTypeConfiguration());
        modelBuilder.Configurations.Add(new ProductSpecificationsTypeConfiguration());

        base.OnModelCreating(modelBuilder);
    }
}
4

1 回答 1

4

db.Select(x => x.Lft)int在子句中返回一个列表,where您将不会访问任何属性。

我想你可能会切换selectwhere实现你想要的。假设db是实际的context.

db.Where(x => x.DepartmentId == id).Select(x => x.Lft)

这有点奇怪。通常它应该看起来像

db.context.Departments.Where(x => x.DepartmentId == id).Select(x => x.Lft)
于 2013-08-15T03:10:16.260 回答