Asp.net
‘System.Data.Entity.Internal.AppConfig’ 的類型初始化程序引發異常
我在 MVC 4 aspnet 網站中使用 EF5。在本地,一切正常,但是當我將其發佈到 IIS 並嘗試進入時,我得到了錯誤
“‘System.Data.Entity.Internal.AppConfig’ 的類型初始化程序引發了異常。”
詳細異常
為 entityFramework 創建配置節處理程序時出錯:在應用程序配置中多次指定 DbContext 類型“GdpSoftware.Server.Data.GdpSoftwareDbContext, GdpSoftware.Server.Data”的配置。每個上下文只能配置一次。(E:\App\web.config 第 104 行)
我在 StackOverflow 中檢查了上一個問題,並且我已經通過 Nuget EntityFramework 解除安裝並重新安裝,並檢查了每個項目中對它的每個引用都是 EF5。我還檢查了每個項目中選擇的框架是 4.5。
任何想法,可能是什麼導致了這個問題?謝謝!吉列爾莫。
網路配置
我需要將 defaultConnectionFactory 更改為 SqlConnectionFactory 而不是預設值
<entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"> <parameters> <parameter value="<My Connection String>" /> </parameters> </defaultConnectionFactory> </entityFramework><http://blogs.msdn.com/b/davidobando/archive/2012/08/14/changing-ef-s-default-provider-from-localdb-to-sql-server.aspx>
在 App.config 文件中執行以下操作,
- 把
connectionStrings元素放在元素之後configSections。- 將元素放在
startup元素之後connectionStrings。<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </configSections> <connectionStrings> <add name="SchedulingContext" connectionString="Data Source=XXX\SQL2008R2DEV;Initial Catalog=YYY;Persist Security Info=True;User ID=sa;Password=XXX" providerName="System.Data.SqlClient"/> </connectionStrings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>