Entity Framework Core 2 – Skalarfunktionszuordnung

Entity Framework Core 2 – Skalarfunktionszuordnung

Entity Framework Core 2 wurde am 14. August veröffentlicht. Es brachte neue Funktionen.

In diesem Artikel werde ich eine davon erklären:Mapping von Skalarfunktionen
Endlich! wir können SQL SERVER verwenden Die Skalarfunktion in LINQ to Entities !

Wie funktioniert es?

Sie müssen statisch deklariert werden und müssen die eingehenden/ausgehenden Parameter genau berücksichtigen.
Sie müssen auch für die statische Methode ein Attribut namens DbFunction deklarieren die den Namen der Skalarfunktion und ihres Schemas, zu dem sie gehört, als Parameter nimmt.

Beispiel:

[DbFunction("ufnGetStock", "dbo")]
 public static int GetProductStock(int productId)
 {
    throw new NotImplementedException();
 }

Microsoft hat diese Methode in seinen Beispielen im DbContext implementiert was zum Beispiel ergibt:

public class AdventureWorksContext : DbContext
{
   public virtual DbSet<Product> Products { get; set; }

   [DbFunction("ufnGetStock", "dbo")]
   public static int GetProductStock(int productId)
   {
      throw new NotImplementedException();
   }

   protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
   {
      optionsBuilder.UseSqlServer(@const.connectionString);

      var lf = new LoggerFactory();
      lf.AddProvider(new MyLoggerProvider());
      optionsBuilder.UseLoggerFactory(lf);
    }

   protected override void OnModelCreating(ModelBuilder modelBuilder)
   {
      modelBuilder.HasDefaultSchema("Production");
      modelBuilder.ApplyConfiguration(new ProductConfiguration());
      base.OnModelCreating(modelBuilder);
   }
}

Sie können diese statische Methode definitiv an anderer Stelle wie :

implementieren
  • Eine statische Methode innerhalb einer klassischen statischen Klasse
  • Eine Erweiterungsmethode für die Zielentität, mit der die Skalarfunktion arbeitet

Beispiele:

public static class ScalarFunctionsHelpers
{
   [DbFunction("ufnGetStock", "dbo")]
    public static int GetProductStock(int productId)
    {
       throw new NotImplementedException();
    }
}

public static class ScalarFunctionsExtentions
{
   [DbFunction("ufnGetStock", "dbo")]
    public static int GetProductStock(this Product product, int productId)
    {
       throw new NotImplementedException();
    }
}

Verwendung dieser 3 Szenarien:

public int GetProductStock(int productId)
{
   // DbContext example
   var query = _context.Products
   .Where(x => x.ProductID == productId)
   .Select(d => AdventureWorksContextDI.GetProductStock(d.ProductID));

   // Exemple of externalized in static class as static function
   query = _context.Products
   .Where(x => x.ProductID == productId)
   .Select(d=> ScalarFunctionsHelpers.GetProductStock(d.ProductID));

   // Exemple of externalized in static class as extension method
   query = _context.Products
   .Where(x => x.ProductID == productId)
   .Select(d => d.GetProductStock(d.ProductID));

   return query.FirstOrDefault();
}

Nettes Feature oder? 😉