Entity framework 6和Sql server 2022无法连接

问题描述 投票:0回答:0

我有简单的控制台应用程序,我无法使用 DBContext(实体框架 6.0.0)、Sql server 2022 连接到数据库

public class TestContext : DbContext
{
    public TestContext(string connectionString) :base(connectionString)
    {
        
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);       
    }
}

使用 SqlConnection 连接工作得很好,但 EF6 没有。没有错误。

static void Main(string[] args)
{
    SqlConnectionStringBuilder cb = new SqlConnectionStringBuilder();
    cb.DataSource = @"***";
    cb.InitialCatalog = @"EF6Test";
    cb.IntegratedSecurity = true;

    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = cb.ConnectionString;
    conn.Open(); //connection OK.

    try
    {
        using (var ctx = new TestContext(cb.ConnectionString))
        {
            var s = ctx.Database.Connection.State;
            ;
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

当我断点时,这里只有错误:

编辑:App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <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>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>
entity-framework
© www.soinside.com 2019 - 2024. All rights reserved.