我有简单的控制台应用程序,我无法使用 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>