title: How to configurate the Entity Famework Core with Azure Functions (Timer trigger) and azure sql?
body: <p>I actually have a database in Azure Sql and I have an Azure function (timer trigger) in another Azure account (different from the database) I have already used Core Entity framework to map the database but when I want to do queries it doesn’t work,so there’s something wrong with the configuration.I tried a lot and tried some solutions but I still couldn’t solve this problem. I’m uploading the code hoping you can help me. Thanks in advance.</p>
  <p><strong><strong>Function1.cs</strong>:</strong></p>
  <pre><code>using System;
  using System.Data.SqlClient;
  using System.Threading.Tasks;
  using System.Collections.Generic;
  using System.Text;
  using Microsoft.Azure.WebJobs;
  using Microsoft.Extensions.Logging;
  using Microsoft.Azure.Functions.Extensions;
  using Microsoft.Net.Http;
  using Microsoft.EntityFrameworkCore.SqlServer.Design;
  using Microsoft.EntityFrameworkCore.SqlServer;
  using Microsoft.EntityFrameworkCore;
  using System.Linq;
  using Newtonsoft.Json;
  
  
  
  
  
  namespace azuretosharepoint
  {
  
      public static class Function1
      {
          [FunctionName(&quot;Function1&quot;)]
          public static async Task RunAsync([TimerTrigger(&quot;0 */1 * * * *&quot;)] TimerInfo myTimer, ILogger log)
          {
              var azsh = new ProgettokpidatabaseContext();
              var listsharepoint = azsh.KpiTables.ToList();
              Console.WriteLine(listsharepoint);
                              
              
           }
      }
  }
  </code></pre>
  <p><strong><strong>progettokpidatabaseContext.cs</strong>:</strong></p>
  <pre><code>using System;
  using Microsoft.EntityFrameworkCore;
  using Microsoft.EntityFrameworkCore.Metadata;
  using Microsoft.EntityFrameworkCore.Design;
  using System.Collections.Generic;
  using System.Text;
  using Newtonsoft.Json;
  
  #nullable disable
  
  namespace azuretosharepoint
  {
      public partial class ProgettokpidatabaseContext : DbContext
      {
          public ProgettokpidatabaseContext()
          {
          }
  
          public ProgettokpidatabaseContext(DbContextOptions&lt;ProgettokpidatabaseContext&gt; options)
              : base(options)
          {
          }
  
          public virtual DbSet&lt;KpiTable&gt; KpiTables { get; set; }
          public virtual DbSet&lt;KptKpiTarget&gt; KptKpiTargets { get; set; }
          public virtual DbSet&lt;KpvKpiValue&gt; KpvKpiValues { get; set; }
  
          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
          {
              if (!optionsBuilder.IsConfigured)
              {
                  optionsBuilder.UseSqlServer(Environment.GetEnvironmentVariable(&quot;sqlconnectionstring&quot;));
                 //#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
                  //optionsBuilder.UseSqlServer(&quot;Server=tcp:progetto-kpi.database.windows.net,1433;Initial Catalog=progetto-kpi-database;Persist Security Info=False;User ID=progetto-kpi;Password=**********;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;&quot;);
              }
          }
  
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              modelBuilder.HasAnnotation(&quot;Relational:Collation&quot;, &quot;SQL_Latin1_General_CP1_CI_AS&quot;);
  
              modelBuilder.Entity&lt;KpiTable&gt;(entity =&gt;
              {
                  entity.HasKey(e =&gt; e.KpiId);
  
                  entity.ToTable(&quot;KPI_table&quot;);
  
                  entity.Property(e =&gt; e.KpiId)
                      .ValueGeneratedNever()
                      .HasColumnName(&quot;KPI_ID&quot;);
  
                  entity.Property(e =&gt; e.KpiBusinessArea)
                      .IsRequired()
                      .HasMaxLength(50)
                      .HasColumnName(&quot;KPI_Business_Area&quot;);
  
                  entity.Property(e =&gt; e.KpiDescription)
                      .HasMaxLength(250)
                      .HasColumnName(&quot;KPI_Description&quot;);
  
                  entity.Property(e =&gt; e.KpiDoi)
                      .HasColumnType(&quot;datetime&quot;)
                      .HasColumnName(&quot;KPI_DOI&quot;)
                      .HasDefaultValueSql(&quot;(getdate())&quot;);
  
                  entity.Property(e =&gt; e.KpiDou)
                      .HasColumnType(&quot;datetime&quot;)
                      .HasColumnName(&quot;KPI_DOU&quot;)
                      .HasDefaultValueSql(&quot;(getdate())&quot;);
  
                  entity.Property(e =&gt; e.KpiFrequency)
                      .HasMaxLength(10)
                      .HasColumnName(&quot;KPI_Frequency&quot;);
  
                  entity.Property(e =&gt; e.KpiManual).HasColumnName(&quot;KPI_Manual&quot;);
  
                  entity.Property(e =&gt; e.KpiName)
                      .IsRequired()
                      .HasMaxLength(50)
                      .HasColumnName(&quot;KPI_Name&quot;);
              });
  
              modelBuilder.Entity&lt;KptKpiTarget&gt;(entity =&gt;
              {
                  entity.HasKey(e =&gt; e.KptId)
                      .HasName(&quot;PK_KPT_table&quot;);
  
                  entity.ToTable(&quot;KPT_KPI_Targets&quot;);
  
                  entity.Property(e =&gt; e.KptId)
                      .ValueGeneratedNever()
                      .HasColumnName(&quot;KPT_ID&quot;);
  
                  entity.Property(e =&gt; e.KptDate)
                      .HasColumnType(&quot;date&quot;)
                      .HasColumnName(&quot;KPT_Date&quot;);
  
                  entity.Property(e =&gt; e.KptDoi)
                      .HasColumnType(&quot;datetime&quot;)
                      .HasColumnName(&quot;KPT_DOI&quot;)
                      .HasDefaultValueSql(&quot;(getdate())&quot;);
  
                  entity.Property(e =&gt; e.KptDou)
                      .HasColumnType(&quot;datetime&quot;)
                      .HasColumnName(&quot;KPT_DOU&quot;)
                      .HasDefaultValueSql(&quot;(getdate())&quot;);
  
                  entity.Property(e =&gt; e.KptKpiId).HasColumnName(&quot;KPT_KPI_ID&quot;);
  
                  entity.Property(e =&gt; e.KptTarget)
                      .HasColumnType(&quot;decimal(18, 2)&quot;)
                      .HasColumnName(&quot;KPT_Target&quot;);
  
                  entity.HasOne(d =&gt; d.KptKpi)
                      .WithMany(p =&gt; p.KptKpiTargets)
                      .HasForeignKey(d =&gt; d.KptKpiId)
                      .OnDelete(DeleteBehavior.ClientSetNull)
                      .HasConstraintName(&quot;FK__KPT_KPI_T__KPT_K__2FCF1A8A&quot;);
              });
  
              modelBuilder.Entity&lt;KpvKpiValue&gt;(entity =&gt;
              {
                  entity.HasKey(e =&gt; e.KpvId)
                      .HasName(&quot;PK_KPV_table&quot;);
  
                  entity.ToTable(&quot;KPV_KPI_Values&quot;);
  
                  entity.Property(e =&gt; e.KpvId)
                      .ValueGeneratedNever()
                      .HasColumnName(&quot;KPV_ID&quot;);
  
                  entity.Property(e =&gt; e.KpvDate)
                      .HasColumnType(&quot;date&quot;)
                      .HasColumnName(&quot;KPV_Date&quot;);
  
                  entity.Property(e =&gt; e.KpvDoi)
                      .HasColumnType(&quot;datetime&quot;)
                      .HasColumnName(&quot;KPV_DOI&quot;)
                      .HasDefaultValueSql(&quot;(getdate())&quot;);
  
                  entity.Property(e =&gt; e.KpvDou)
                      .HasColumnType(&quot;datetime&quot;)
                      .HasColumnName(&quot;KPV_DOU&quot;)
                      .HasDefaultValueSql(&quot;(getdate())&quot;);
  
                  entity.Property(e =&gt; e.KpvKpiId).HasColumnName(&quot;KPV_KPI_ID&quot;);
  
                  entity.Property(e =&gt; e.KpvValue)
                      .HasColumnType(&quot;decimal(18, 2)&quot;)
                      .HasColumnName(&quot;KPV_Value&quot;);
  
                  entity.HasOne(d =&gt; d.KpvKpi)
                      .WithMany(p =&gt; p.KpvKpiValues)
                      .HasForeignKey(d =&gt; d.KpvKpiId)
                      .OnDelete(DeleteBehavior.ClientSetNull)
                      .HasConstraintName(&quot;FK__KPV_KPI_V__KPV_K__3587F3E0&quot;);
              });
  
              OnModelCreatingPartial(modelBuilder);
          }
  
          partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
      }
  </code></pre>
  <p>}</p>
  <p><strong>local.settings.json :</strong></p>
  <pre><code>{
    &quot;IsEncrypted&quot;: false,
    &quot;AzureWebJobsStorage&quot;: &quot;UseDevelopmentStorage=true&quot;,
    &quot;FUNCTIONS_WORKER_RUNTIME&quot;: &quot;dotnet&quot;,
    &quot;Values&quot;: {
      &quot;sqlconnectionstring&quot;: &quot;Server=tcp:progetto-kpi.database.windows.net,1433;Initial Catalog=progetto-kpi-database;Persist Security Info=False;User ID=progetto-kpi;Password=*********;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;&quot;,
      &quot;ProviderName&quot;: &quot;System.Data.SqlClient&quot;
  
  
    }
  }
  </code></pre>
  <p><strong>the problem :</strong></p>
  <pre><code>Azure Functions Core Tools
  Core Tools Version:       3.0.3568 Commit hash: e30a0ede85fd498199c28ad699ab2548593f759b  (64-bit)
  Function Runtime Version: 3.0.15828.0
  
  Can't determine project language from files. Please use one of [--csharp, --javascript, --typescript, --java, --python, --powershell, --custom]
  Missing value for AzureWebJobsStorage in local.settings.json. This is required for all triggers other than httptrigger, kafkatrigger, rabbitmqtrigger, orchestrationTrigger, activityTrigger, entityTrigger. You can run 'func azure functionapp fetch-app-settings &lt;functionAppName&gt;' or specify a connection string in local.settings.json.
  </code></pre>
  <p><strong>azuretosharepoint.csproj :</strong></p>
  <pre><code>&lt;Project Sdk=&quot;Microsoft.NET.Sdk&quot;&gt;
    &lt;PropertyGroup&gt;
      &lt;TargetFramework&gt;netcoreapp3.1&lt;/TargetFramework&gt;
      &lt;AzureFunctionsVersion&gt;v3&lt;/AzureFunctionsVersion&gt;
      &lt;UserSecretsId&gt;4e8db286-9c45-4574-b556-eb0a04ac345e&lt;/UserSecretsId&gt;
      &lt;OutputType&gt;Library&lt;/OutputType&gt;
    &lt;/PropertyGroup&gt;
    &lt;ItemGroup&gt;
      &lt;PackageReference Include=&quot;Dapper&quot; Version=&quot;2.0.90&quot; /&gt;
      &lt;PackageReference Include=&quot;Dapper.Contrib&quot; Version=&quot;2.0.78&quot; /&gt;
      &lt;PackageReference Include=&quot;Dapper.FluentMap&quot; Version=&quot;2.0.0&quot; /&gt;
      &lt;PackageReference Include=&quot;Dapper.SimpleCRUD&quot; Version=&quot;2.3.0&quot; /&gt;
      &lt;PackageReference Include=&quot;EntityFramework&quot; Version=&quot;6.4.4&quot; /&gt;
      &lt;PackageReference Include=&quot;Microsoft.ApplicationInsights.WorkerService&quot; Version=&quot;2.18.0-beta1&quot; /&gt;
      &lt;PackageReference Include=&quot;Microsoft.AspNetCore.AzureKeyVault.HostingStartup&quot; Version=&quot;2.0.4&quot; /&gt;
      &lt;PackageReference Include=&quot;Microsoft.Azure.Functions.Extensions&quot; Version=&quot;1.1.0&quot; /&gt;
      &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore&quot; Version=&quot;5.0.1&quot; /&gt;
      &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.Design&quot; Version=&quot;5.0.1&quot;&gt;
        &lt;PrivateAssets&gt;all&lt;/PrivateAssets&gt;
        &lt;IncludeAssets&gt;runtime; build; native; contentfiles; analyzers; buildtransitive&lt;/IncludeAssets&gt;
      &lt;/PackageReference&gt;
      &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.Relational&quot; Version=&quot;5.0.1&quot; /&gt;
      &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.SqlServer&quot; Version=&quot;5.0.1&quot; /&gt;
      &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.SqlServer.Design&quot; Version=&quot;2.0.0-preview1-final&quot; /&gt;
      &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.Tools&quot; Version=&quot;5.0.1&quot;&gt;
        &lt;PrivateAssets&gt;all&lt;/PrivateAssets&gt;
        &lt;IncludeAssets&gt;runtime; build; native; contentfiles; analyzers; buildtransitive&lt;/IncludeAssets&gt;
      &lt;/PackageReference&gt;
      &lt;PackageReference Include=&quot;Microsoft.Extensions.Configuration.UserSecrets&quot; Version=&quot;3.1.10&quot; /&gt;
      &lt;PackageReference Include=&quot;Microsoft.NET.Sdk.Functions&quot; Version=&quot;3.0.12&quot; /&gt;
      &lt;PackageReference Include=&quot;System.Configuration.ConfigurationManager&quot; Version=&quot;6.0.0-preview.4.21253.7&quot; /&gt;
      &lt;PackageReference Include=&quot;System.Data.Common&quot; Version=&quot;4.3.0&quot; /&gt;
      &lt;PackageReference Include=&quot;System.Data.SqlClient&quot; Version=&quot;4.8.2&quot; /&gt;
      &lt;PackageReference Include=&quot;System.IO&quot; Version=&quot;4.3.0&quot; /&gt;
      &lt;PackageReference Include=&quot;System.Linq&quot; Version=&quot;4.3.0&quot; /&gt;
      &lt;PackageReference Include=&quot;System.Runtime&quot; Version=&quot;4.3.1&quot; /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;None Update=&quot;host.json&quot;&gt;
        &lt;CopyToOutputDirectory&gt;PreserveNewest&lt;/CopyToOutputDirectory&gt;
      &lt;/None&gt;
      &lt;None Update=&quot;local.settings.json&quot;&gt;
        &lt;CopyToOutputDirectory&gt;Always&lt;/CopyToOutputDirectory&gt;
        &lt;CopyToPublishDirectory&gt;Never&lt;/CopyToPublishDirectory&gt;
      &lt;/None&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;Service Include=&quot;{508349b6-6b84-4df5-91f0-309beebad82d}&quot; /&gt;
    &lt;/ItemGroup&gt;
  &lt;/Project&gt;
  </code></pre>