How to catch daughter entities from a data constraint?

Asked

Viewed 43 times

0

Well my problem is this, I need to bring a bank entity and it contains several lists that should bring your data following a rule, the field data_exclusao must be void.

All entities have the field data_exclusao which represents the logical deletion of the data. Then follows an example to get better I have the entity person and it contains the lists of entities endereco and phone. When I go to fetch the person entity from the bank it should bring all the addresses and telephones where the data_exclusao is equal to null.

I use the EF and I would like to know where I apply this rule so that it is generic, that is, all searches of the database always bring the data where the field data_exclusao is equal to null.

Observing: The data model through the EDMX and the version of EF what use is the EF6.

  • The version of your Entity Framework please?

  • @Virgilionovic the version I’m wearing is EF6

  • is roughly: https://www.codeguru.com/csharp/soft-deleting-entities-cleanly-using-entity-framework-6-interceptors.html

  • 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.

  • Another example: https://stackoverflow.com/questions/34933981/entity-framework-soft-delete-implementation-using-database-interceptor-notworki

  • Thanks, I’ll take a look now.

  • It worked out William?

  • 1

    I have not had time to implement, once I put the result here.

Show 3 more comments

1 answer

0


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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.