locked
What is the meaning of: Database.Log = (sql) => Debug.Write(sql)? RRS feed

  • Question

  • User-1350042179 posted

    Hi

    I have the configuration in EF:

     public class AuraDbContext : DbContext, IQueryableUnitOfWork
        {
            public AuraDbContext() : base("name=AuraDbConnectionString")
            {
                Database.Log = (sql) => Debug.Write(sql);
            }
    
            public virtual DbSet<Archivo> Archivo { get; set; }
            public virtual DbSet<Bien> Bien { get; set; }
            public virtual DbSet<BienMaterial> BienMaterial { get; set; }
            public virtual DbSet<BienSerie> BienSerie { get; set; }
            public virtual DbSet<BienSerieComponente> BienSerieComponente { get; set; }
            public virtual DbSet<PruebaSAT> PruebasSAT { get; set; }
            public virtual DbSet<Contrato> Contrato { get; set; }
            public virtual DbSet<ContratoBien> ContratoBien { get; set; }
            public virtual DbSet<ContratoBienMaterial> ContratoBienMaterial { get; set; }
            public virtual DbSet<ContratoBienMaterialBom> ContratoBienMaterialBom { get; set; }
            public virtual DbSet<ContratoBienMaterialCaracteristicaTecnica> ContratoBienMaterialCaracteristicaTecnica { get; set; }
            public virtual DbSet<ContratoGarantia> ContratoGarantia { get; set; }
            public virtual DbSet<ContratoGrupoBom> ContratoGrupoBom { get; set; }
            public virtual DbSet<ContratoGrupoBomBien> ContratoGrupoBomBien { get; set; }
            public virtual DbSet<ContratoGrupoContractual> ContratoGrupoContractual { get; set; }
            public virtual DbSet<ContratoGrupoContractualBien> ContratoGrupoContractualBien { get; set; }
            public virtual DbSet<ContratoGrupoContractualBienMaterial> ContratoGrupoContractualBienMaterial { get; set; }
            //public virtual DbSet<ContratoProyecto> ContratoProyecto { get; set; }
            public virtual DbSet<Parametro> Parametros { get; set; }
            public virtual DbSet<ParametroValor> ParametroValores { get; set; }
            public virtual DbSet<Proveedor> Proveedor { get; set; }

    What is the meaning of: Database.Log = (sql) => Debug.Write(sql)?

    Wednesday, February 13, 2019 3:31 PM

Answers

  • User-1350042179 posted

    I  see that It shows the sql generated by Entity Framework in the output:

    alt

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 13, 2019 7:29 PM

All replies

  • User753101303 posted

    Hi,

    See https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/anonymous-functions#the-evolution-of-delegates-in-c

    IMHO the easiest way to understand this is that the purpose of this is to use code the same way you are using data.

    That is here you assign to a delegate property a "data" which is nothing else than the piece of code that should run to log SQL statements. As a result you should see all SQL statements on the VS output window.

    You'll see that as well when using EF. You'll pass for example this kind of "lambda expression" to the Where method this time as an "expression tree". In addition to running maybe this code, the Where method can then inspect this expression and translate this C# code to the corresponding SQL WHERE clause.

    Similarly you could use that in your own code when you need basically to pass or assign "code" to a property or as an argument to a method so that this code can later be analyzed and/or run.

    Wednesday, February 13, 2019 4:08 PM
  • User-1350042179 posted

    I  see that It shows the sql generated by Entity Framework in the output:

    alt

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 13, 2019 7:29 PM