2014年7月24日 星期四

EF4->EF6,執行時出現 「The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.」訊息

 最近為了要用到log等機制及把entity 從web project 獨立成為函數庫的專業以供日後他案應用
索興把EF從4升到EF6,不過在執行測試時,出現以下的訊息:
===========================================================
The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
===========================================================
那會安爾生~~~!?
anyway,先找solution吧,
參考以下的連結,照他吩咐去做就是了:

1.把app.config (是caller project的app.config,不是entity 所在的project的app.confiig)的
<entityFramework>區塊拿掉

2.DbContext子類別--EntityContext類別提供一個public static getDbContext的函數,取代其建構元
   (能不能把建構元設為 inner或是private的層次?沒試,要試才知)
 ex:
   ==============================
 public static ERPEntities getDbContext() {
            if (!isEF6Inited) {
                // EF 6.0.1 throws an exception, unless we first probe the provider types.
                var type1 = typeof(System.Data.Entity.SqlServer.SqlProviderServices);
                //var type2 = typeof(System.Data.Entity.SqlServerCompact.SqlCeProviderServices);
            }
            return new ERPEntities();
        }
   ==============================
2.在repository(也就是呼叫/操作Entity Context的商業邏輯Facade類別)中,
  把原本呼叫new xxxEntity()的方式改由從 getDbContext()的方式取得DbContext


以上,大功告成,祝大家開發愉快了