[英]typeinitializationexception was unhandled .The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception
I have created a Entity Framework DLL EMPDAL which points to Employees Table of Northwnd database. 我创建了一个指向Northwnd数据库的Employees Table的Entity Framework DLL EMPDAL。 Below is the code for Entity framework
以下是实体框架的代码
namespace EmpDAL{
public class EmplooyeeData
{
public static List<Employee> GetEmployees( int EmployeeId)
{
using (DbEntities dbContext = new DbEntities())
{
return dbContext.Employees.Where(x => x.EmployeeID == EmployeeId).ToList();
}
}
public static void SaveEmployee(Employee emp)
{
DbEntities dbContext = new DbEntities();
dbContext.Employees.Add(emp);
dbContext.SaveChanges();
}
}}
Below is the Appconfig file of EMPDAL 以下是EMPDAL的Appconfig文件
<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>
<connectionStrings>
<add name="DbEntities" connectionString="metadata=res://*/EmployeeModel.csdl|res://*/EmployeeModel.ssdl|res://*/EmployeeModel.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost;initial catalog=NorthWnd;user id=sa;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
</configuration>
Below is the code for the WCF service 以下是WCF服务的代码
namespace EmployeeService{
public class EmployeeService : IEmployeeService
{
public List<EmpDAL.Employee> GetEmployees(int Empid)
{
return EmpDAL.EmplooyeeData.GetEmployees(Empid);
}
public void SaveChanges(EmpDAL.Employee emp)
{
EmpDAL.EmplooyeeData.SaveEmployee(emp);
}
}}
Below is the AppConfig for WCF EmployeeService 以下是WCF EmployeeService的AppConfig
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="EmployeeService.EmployeeService">
<endpoint address="" binding="basicHttpBinding" contract="EmployeeService.IEmployeeService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/Design_Time_Addresses/EmployeeService/EmployeeService/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel> <configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <connectionStrings>
<add name="DbEntities"connectionString="metadata=res://*/EmployeeModel.csdl|res://*/EmployeeModel.ssdl|res://*/EmployeeModel.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost;initial catalog=NorthWnd;user id=sa;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory,EntityFramework" /> </entityFramework></configuration>
When client uses the WCF service it and when it tries to execute "using (DbEntities dbContext = new DbEntities())" it goes to 当客户端使用WCF服务时,并尝试执行“使用(DbEntities dbContext = new DbEntities())”时,它将转到
public partial class DbEntities : DbContext {
public DbEntities()
: base("name=DbEntities")
and throws exception 并引发异常
typeinitializationexception was unhandled .The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.
It is very generic exception so in your editor try reading the whole details of the exception and then resolve it. 这是非常通用的异常,因此请在您的编辑器中尝试阅读异常的全部细节,然后加以解决。 For me, it was due to the entity framework version written in webconfig file and actual file included ,there was difference and also i had to remove providers tag in web config file of my project
对我来说,这是由于实体框架版本写在webconfig文件中,并且包含实际文件,两者之间存在差异,而且我不得不在我的项目的web配置文件中删除了provider标签
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.