0

Without using .net core, I worked on an existing .net framework 4.8 Console project that's missing app.config, trying to replicate the tutorial on how to create a dbContext to SQL Server Express. So I've added an app.config manually by googling, setting the values as I see fit, but it seems something is still missed or I need to add something.

Below is my code so far that throws the error, I was hoping to see "TestDB" database in App_Data be created when I simply add a single row.

System.Data.Entity.Core.ProviderIncompatibleException: 'An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.'

Config file:

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
  </startup>
    <connectionStrings>
        <add name="TestDbContextConString"
             connectionString="Data Source=DESKTOP-OEQUB30\SQLEXPRESS; Initial Catalog=TestDB;
      Integrated Security=True;MultipleActiveResultSets=False" providerName="System.Data.SqlClient" />
    </connectionStrings>    
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>    

    

TestClass.cs

public class TestClass
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }    
}

public class TestDataDbContext : DbContext
{
    public TestDataDbContext() : base("TestDbContextConString")
    {
    }

    public DbSet<TestClass> TestClasses { get; set; }
}

Program.cs

public class Program
{
    static void Main(string[] args)
    {
        using(var context = new TestDataDbContext())
        {
            context.TestClasses.Add(new TestClass
            {
                Name = "John Smith",
                Age = 34
            });

            context.SaveChanges();  
        }
    }
}

SQL Server Express

Datadump
  • 45
  • 6
  • I have no idea what that version of .net core expects in the connection string but _please patch your machine_ - you're missing two and a half years of patches. You can get the latest SQL Server 2019 CU [here](https://www.microsoft.com/en-us/download/details.aspx?id=100809). – Aaron Bertrand Apr 01 '22 at 07:12
  • Isn't this the latest, or close to it? It's 2019: Microsoft SQL Server 2019 (RTM) - 15.0.2000.5 (X64) Sep 24 2019 13:48:23 Copyright (C) 2019 Microsoft Corporation Express Edition (64-bit) on Windows 10 Home 10.0 (Build 19043: ) – Datadump Apr 01 '22 at 07:37
  • No. For the first year after a major SQL Server release, Microsoft releases monthly patches, and after that every other month for the life of that version. These patches include security fixes, performance enhancements, and other updates you benefit from. You’ve missed 15 of these but it’s not too late to apply the most recent one now. You apply Windows Updates regularly, right? How about your phone? SQL Server needs the same kind of attention. – Aaron Bertrand Apr 01 '22 at 07:41
  • So do you think that sql update or the lack of it has something to do with the exception I'm getting? The project's target framework is 4.8 and not core. – Datadump Apr 01 '22 at 07:46
  • No. Just pointing out that it’s best practice to keep your stuff patched instead of ignoring it for 2+ years. – Aaron Bertrand Apr 01 '22 at 07:48
  • Thanks, I just installed that SQL today, didn't know it needed patching. – Datadump Apr 01 '22 at 07:58

0 Answers0