【发布时间】:2014-10-23 23:37:13
【问题描述】:
我有 2 个表 Categories 和 Images 类别是自引用的 ParentId 作为 Nullable Foreign Key
代码优先类
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 中做到这一点
【问题讨论】:
-
你试过了吗?
-
是的,我尝试了
Linq和LinqKit,但我无法过滤图像我认为在 SQL Server 中创建Stored Procuder会更容易
标签: c# sql-server linq ef-code-first linqkit