NHibernate has a mapping wherethat allows you to specify a condition for a property mapping that affects how it is retrieved from the database. For example, if I wanted to implement soft deletion and exclude all deleted items from the set, I could display it like this:
Fluentnhibernate
HasMany(x => x.Children).Where("IsDeleted = 0");
hbm.xml
<class name="Parent" table="[Parents]">
<bag cascade="all" lazy="true" name="Children" where="IsDeleted = 0">
</bag>
</class>
Is there something similar in Entity Framework 6?
The closest I found is a library called EntityFramework.Filters , which allows you to add global filters for a property, but it doesn't seem to work when this property is a collection.
, , , , , (.. ). :
public class ReportOutline
{
public long Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public virtual ICollection<OutlineItem> OutlineItems { get; set; }
}
public class OutlineItem
{
public long Id { get; set; }
public string Name { get; set; }
public long ReportOutlineId { get; set; }
public long? ParentOutlineItemId { get; set; }
public virtual ReportOutline ReportOutline { get; set; }
public virtual OutlineItem ParentOutlineItem { get; set; }
public virtual ICollection<OutlineItem> OutlineItems { get; set; }
}
API- EF, :
modelBuilder.Entity<ReportOutline>()
.HasKey(o => o.Id)
.HasMany(o => o.OutlineItems)
.WithRequired(i => i.ReportOutline)
.HasForeignKey(i => i.OutlineId);
modelBuilder.Entity<OutlineItem>()
.HasKey(p => p.Id)
.HasMany(p => p.OutlineItems)
.WithOptional(c => c.ParentOutlineItem)
.HasForeignKey(c => c.ParentOutlineItemId);
, . , OutlineItems ReportOutline, ( ):
Id Name ReportOutlineId ParentOutlineItemId
1 Introduction 1 NULL
2 Pets 1 NULL
3 Cats 1 2
4 Dogs 1 2
ReportOutline DbContext, , ReportOutlineId Id, ReportOutline.OutlineItems . , , :
Title: My Report
Author: valverij
I. Introduction (Id: 1)
II. Pets (Id: 2)
A. Cats (Id: 3)
B. Dogs (Id: 4)
III. Cats (Id: 3) <--- Duplicated
IV. Dogs (Id: 4) <--- Duplicated
, NHibernate FluentNhibernate, where , ReportOutline.OutlineItems :
HasMany(x => x.OutlineItems).Where("ParentOutlineItemId IS NULL");
ReportOutline , OutlineItem.