我有 2 个表Categories
和类别作为可空外键Images
自引用ParentId
代码第一类
public class Category {
public int Id { get; set; }
public string Name{ get; set; }
public int? ParentId { get; set; }
public bool IsDeleted { get; set; }
public byte[] Timestamp { get; set; }
public virtual Category Parent { get; set; }
public virtual ICollection<Category> Parents { get; set; }
public virtual ICollection<Image> Images { get; set; }
}
Public class Image {
public int Id { get; set; }
public int? CategoryId { get; set; }
public string Source { get; set; }
public string Description { get; set; }
public bool IsDeleted { get; set; }
public byte[] Timestamp { get; set; }
// Foreign keys
public virtual Category Category { get; set; }
}
public class CategoryMap : EntityTypeConfiguration<Category> {
public CategoryMap() {
// Primary Key
HasKey(t => t.Id);
// Properties
Property(t => t.Name).IsRequired().HasMaxLength(250);
Property(t => t.Timestamp).IsRequired().IsFixedLength().HasMaxLength(8).IsRowVersion();
// Table & Column Mappings
ToTable("Category");
Property(t => t.Id).HasColumnName("Id");
Property(t => t.ParentId).HasColumnName("ParentId");
Property(t => t.Name).HasColumnName("Category");
Property(t => t.IsDeleted).HasColumnName("IsDeleted");
Property(t => t.Timestamp).HasColumnName("Timestamp");
// Relationships
HasOptional(t => t.Parent).WithMany(t => t.Parents)
.HasForeignKey(d => d.ParentId)
.WillCascadeOnDelete(false);
}
public class ProfileImageMap : EntityTypeConfiguration<ProfileImage> {
public ProfileImageMap() {
// Primary Key
HasKey(t => t.Id);
// Properties
Property(t => t.Source).IsRequired().HasMaxLength(255);
Property(t => t.Description).HasMaxLength(255);
Property(t => t.Timestamp).IsRequired().IsFixedLength().HasMaxLength(8).IsRowVersion();
// Table & Column Mappings
ToTable("Images");
Property(t => t.Id).HasColumnName("Id");
Property(t => t.CategoryId ).HasColumnName("CategoryId ");
Property(t => t.Source).HasColumnName("Source");
Property(t => t.Description).HasColumnName("Description");
Property(t => t.IsDeleted).HasColumnName("IsDeleted");
Property(t => t.Timestamp).HasColumnName("Timestamp");
// Relationships
HasOptional(t => t.Category).WithMany(t => t.Images)
.HasForeignKey(d => d.CategoryId);
}
}
** 上下文**
public DbSet<Category> Categories { get; set; }
public DbSet<Image> Images { get; set; }
问题是我如何构建一个 LINQ 语句以返回基于 Id 的类别与父类别的所有图像,不包括标记为的图像IsDeleted = true
是否可以在LINQ
或LinqKit