We are migration from DataBase first approach to Code first for Entity Framework 6 We have model
public class Parent
{
public virtual int Id { get; set; }
public virtual DateTimeOffset DateFrom { get; set; }
public virtual DateTimeOffset DateTo { get; set; }
//navigation property
public virtual ICollection<Child> Cildren { get; set; }
}
public class Child {
public int Id { get; set; }
public DateTimeOffset DateFrom { get; set; }
public DateTimeOffset DateTo { get; set; }
//navigation property
public virtual Parent Parent { get; set; }
}
We have several functions in edmx like this (but a little complicated, functions call each other )
<Function Name="IsParentActive" ReturnType="Edm.Boolean">
<Parameter Name="Parent" Type="MyContext.Parent" />
<Parameter Name="time" Type="Edm.DateTimeOffset" Nullable="false" />
<DefiningExpression>
(Parent.DateFrom <= time) AND (Parent.DateTo > time)
</DefiningExpression>
</Function>
<Function Name="IsChildActive" ReturnType="Edm.Boolean">
<Parameter Name="Child" Type="MyContext.Child" />
<Parameter Name="time" Type="Edm.DateTimeOffset" Nullable="false" />
<DefiningExpression>
(Child.DateFrom <= time) AND (Child.DateTo > time) AND
MyContext.IsParentActive(Child.Parent, time)
</DefiningExpression>
</Function>
in C# code we have next mapping
[DbFunction("MyContext", "IsParentActive")]
public static bool IsActive(this Parent parent, DateTimeOffset time) {
return time >= parent.DateFrom && time < parent.DateTo
}
[DbFunction("MyContext", "IsChildActive")]
public static bool IsActive(this Child child, DateTimeOffset time) {
return time >= child.DateFrom && time < child.DateTo
&& child.Parent.IsActive(time);
}
And We can use it in IQueryable in any place
var result = context.Parents.Where(p => p.IsActive(time));
var result = context.Parents.Where(p => p.Cildren.Any(c => !c.IsActive(time));
var result = context.Cildren.Where(p => p.Parent.IsActive(time));
Now I can't find good way to replace this approach.
I tried use C# functions with expression but They not applicable in subqueries (context.Cildren.Where(p => p.Parent.IsActive(time)))