The way to work around a feature that is not yet available in Entity Farmework 6.2.0 is to use the packages :
With these two packages, the feature is automatically filtered in all SQL
of entities configured to run Global filters.
A minimum example:
Entity:
public partial class Funcionario
{
public int Id { get; set; }
public string Nome { get; set; }
public DateTime? DataExclusao { get; set; }
}
How will all SQL
work where the Global filter is DataExclusao IS NULL
, so to work add namespace
:
using Z.EntityFramework.Plus;
In the Contrast of your class that inherits from DbContext
, write this line of code:
this.Filter<Funcionario>(x => x.Where(a => a.DataExclusao == null));
Full Code:
public partial class DatabaseContext : DbContext
{
public DatabaseContext()
:base(@"Data Source=.\SqlExpress; ...")
{
this.Filter<Funcionario>(x => x.Where(a => a.DataExclusao == null));
}
public virtual DbSet<Funcionario> Funcionario { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Funcionario>()
.ToTable("Funcionario");
modelBuilder.Entity<Funcionario>()
.Property(e => e.DataExclusao).HasColumnType("datetime");
modelBuilder.Entity<Funcionario>()
.Property(e => e.Nome)
.IsRequired()
.HasMaxLength(50)
.IsUnicode(false);
}
}
After this configuration, instantiate the class and use, for example:
using (DatabaseContext db = new DatabaseContext())
{
var c = db.Funcionario.ToArray();
}
the SQL
that was debugged is as follows:
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Nome] AS [Nome],
[Extent1].[DataExclusao] AS [DataExclusao]
FROM [dbo].[Funcionario] AS [Extent1]
WHERE [Extent1].[DataExclusao] IS NULL
If you do not want to use the filters at some point in your code, use the AsNoFilter()
extension method, for example:
var c = db.Funcionario.AsNoFilter().ToArray();
Other examples: