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句ではどのプロパティにもアクセスしません。

私はあなたが切り替えselectて、whereあなたが望むものを達成するかもしれないと思います. 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 に答える