The Way to Circumvent a Feature Not So Far in the Version Entity Farmework 6.2.0 is using the packets:
With these two packages is added the feature in filter automatically in all SQL
of the entities configured to run filters Global.
A minimal example:
Entity:
public partial class Funcionario
{
public int Id { get; set; }
public string Nome { get; set; }
public DateTime? DataExclusao { get; set; }
}
How will it all work SQL
where the Global filter is DataExclusao IS NULL
, then to work add the namespace
:
using Z.EntityFramework.Plus;
in the Builder of his class who inherits from the DbContext
, writes that line of code:
this.Filter<Funcionario>(x => x.Where(a => a.DataExclusao == null));
Complete 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 setting, instate the class and use it as an example:
using (DatabaseContext db = new DatabaseContext())
{
var c = db.Funcionario.ToArray();
}
to SQL
which has been 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 don’t want to use filters at some point in your code, use the extension method AsNoFilter()
, example:
var c = db.Funcionario.AsNoFilter().ToArray();
Other examples:
References
The version of your Entity Framework please?
– novic
@Virgilionovic the version I’m wearing is EF6
– Guilherme Caixeta
is roughly: https://www.codeguru.com/csharp/soft-deleting-entities-cleanly-using-entity-framework-6-interceptors.html
– novic
This functionality was adopted by the version
Entity Framework Core
with the name of Global Query Filter where it was automatically run in all Configured Entitdades, in Version 6 has to be a plugin seems to me ... I am looking for.– novic
Another example: https://stackoverflow.com/questions/34933981/entity-framework-soft-delete-implementation-using-database-interceptor-notworki
– novic
Thanks, I’ll take a look now.
– Guilherme Caixeta
It worked out William?
– novic
I have not had time to implement, once I put the result here.
– Guilherme Caixeta