C# 实体框架中的不可修补错误
我正在尝试使用代码优先的方法,通过外部数据库实现实体框架的一个基本示例。我只想从数据库中的表中读取一些已经存在的数据。我实现它的方式是有效的,因为它显示了正确的输出,但在这个过程中会抛出一系列异常。我已经找到了抛出异常的代码行,但我无法使用try/catch捕获它们(请参阅代码) 以下是我运行程序时输出日志(调试)的屏幕转储(我用粉色突出显示了最终输出): 下面是我连接到的表的屏幕转储: 即使输出是正确的,错误似乎也会影响性能。运行代码需要2-3秒,即使该表只包含六列和三行 我做错了什么,让它变成这样 代码 使用EF连接的函数:C# 实体框架中的不可修补错误,c#,sql,sql-server,entity-framework,entity-framework-6,C#,Sql,Sql Server,Entity Framework,Entity Framework 6,我正在尝试使用代码优先的方法,通过外部数据库实现实体框架的一个基本示例。我只想从数据库中的表中读取一些已经存在的数据。我实现它的方式是有效的,因为它显示了正确的输出,但在这个过程中会抛出一系列异常。我已经找到了抛出异常的代码行,但我无法使用try/catch捕获它们(请参阅代码) 以下是我运行程序时输出日志(调试)的屏幕转储(我用粉色突出显示了最终输出): 下面是我连接到的表的屏幕转储: 即使输出是正确的,错误似乎也会影响性能。运行代码需要2-3秒,即使该表只包含六列和三行 我做错了什么,让
public PrintFirstEmployeeName()
{
try
{
UniContext db = new UniContext();
// Exceptions are thrown during this line of code:
string fullName = db.Employees.FirstOrDefault().first_name
+ " " + db.Employees.FirstOrDefault().last_name;
Debug.WriteLine($"Hello { fullName }!");
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
员工模型(名称与数据库名称1:1匹配):
dbContext
实现:
public class UniContext : DbContext
{
public UniContext() : base("Name=MyConnString") { }
public DbSet<EmployeeModel> Employees { get; set; }
}
错误
据我所知,有三种不同的错误。所有这些都被多次抛出
抛出异常:System.Data.dll中的“System.Data.SqlClient.SqlException”
在EntityFramework.dll中引发异常:“System.Data.Entity.Core.EntityCommandExecutionException”
在EntityFramework.SqlServer.dll中引发异常:“System.Data.Entity.Core.EntityCommandExecutionException”
现有数据库通常没有这两个表
dbo.\uu MigrationHistory
和dbo.EdmMetadata
。实体框架(EF)在初始化期间查找这些表,这导致了我看到的异常。在实例化DbContext
之后立即将初始化设置为null
,将告诉EF不要查找这些表,并且不会发生异常
using (UniContext db = new UniContext())
{
Database.SetInitializer<UniContext>(null); // Prevents initialization exceptions
var emp = db.Employees.First();
Debug.WriteLine($"Hello { emp.FirstName } { emp.LastName }!");
}
使用(UniContext db=new UniContext())
{
Database.SetInitializer(null);//防止初始化异常
var emp=db.Employees.First();
Debug.WriteLine($“Hello{emp.FirstName}{emp.LastName}!”);
}
- 有关更多详细信息,请参阅
- 感谢@JohnyL帮我解开了这个谜
<?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=xxx" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="MyConnString" connectionString="Integrated Security = SSPI; Persist Security Info = False; Data Source = MyServer; Database = MyDatabase" providerName="System.Data.SqlClient"/>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlClient" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
using (UniContext db = new UniContext())
{
Database.SetInitializer<UniContext>(null); // Prevents initialization exceptions
var emp = db.Employees.First();
Debug.WriteLine($"Hello { emp.FirstName } { emp.LastName }!");
}