2012-10-24 23 views
1

我已經收到「System.InvalidCastException 不能從源類型轉換爲目標類型。」當嘗試從asp.net mvc3使用單聲道4.5配置文件和postgres在後端訪問數據庫時。單聲道4.5輪廓,實體Npgsql的框架 - NpgsqlSystem.InvalidCastException不能從源類型轉換爲目標類型

這裏是我的代碼如下所示:

public class User 
{ 
    public int Id { get; set; } 
    public string Username { get; set; } 
    public string PasswordHash { get; set; } 
    public string PasswordSalt { get; set; }  
} // class 

AppContext:

using System; 
using System.Data.Entity; 
using Npgsql; 
using NpgsqlTypes; 

namespace Web 
{ 
public class AppContext : DbContext 
{ 
    public DbSet<User> Users { get; set; } 

    protected override void OnModelCreating (DbModelBuilder modelBuilder) 
    { 
     base.OnModelCreating (modelBuilder); 
    } 
} // class 
} // namespace 

數據庫架構(DBO)

CREATE TABLE dbo."Users" 
(
"Id" integer NOT NULL DEFAULT nextval('dbo."Users_Id_seq"'::regclass), 
"Username" character varying(50) NOT NULL, 
"PasswordHash" character varying(255) NOT NULL, 
"PasswordSalt" character varying(128) NOT NULL, 
CONSTRAINT pk_users_id PRIMARY KEY ("Id") 
) 

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     var db = new AppContext(); 
     var users = db.Users.Find(1); 
     ViewData ["Message"] = "Welcome to ASP.NET MVC on Mono!"; 
     return View(); 
    } 
} 

錯誤我得到的是:

System.InvalidCastException 

Cannot cast from source type to destination type. 

Description: HTTP 500.Error processing request. 
Details: Non-web exception. Exception origin (name of application or object): Npgsql. 
Exception stack trace: 
at Npgsql.NpgsqlFactory.CreateConnection() [0x00000] in /private/tmp/source/bockbuild/profiles/mono-mac-release/build-root/mono-2.11/_build/mono-2.11.git/mcs/class/Npgsql/Npgsql/NpgsqlFactory.cs:64 
at System.Data.Entity.Internal.LazyInternalConnection.CreateConnectionFromProviderName (string) [0x00007] in /private/tmp/source/bockbuild/profiles/mono-mac-release/build-root/mono-2.11/_build/mono-2.11.git/external/entityframework/src/EntityFramework/Internal/LazyInternalConnection.cs:379 
at System.Data.Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting (System.Configuration.ConnectionStringSettings) [0x00045] in /private/tmp/source/bockbuild/profiles/mono-mac-release/build-root/mono-2.11/_build/mono-2.11.git/external/entityframework/src/EntityFramework/Internal/LazyInternalConnection.cs:368 
at System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig (string,System.Data.Entity.Internal.AppConfig) [0x0000e] in /private/tmp/source/bockbuild/profiles/mono-mac-release/build-root/mono-2.11/_build/mono-2.11.git/external/entityframework/src/EntityFramework/Internal/LazyInternalConnection.cs:310 
at System.Data.Entity.Internal.LazyInternalConnection.Initialize() [0x00047] in /private/tmp/source/bockbuild/profiles/mono-mac-release/build-root/mono-2.11/_build/mono-2.11.git/external/entityframework/src/EntityFramework/Internal/LazyInternalConnection.cs:242 
at System.Data.Entity.Internal.LazyInternalConnection.get_ProviderName() [0x00000] in /private/tmp/source/bockbuild/profiles/mono-mac-release/build-root/mono-2.11/_build/mono-2.11.git/external/entityframework/src/EntityFramework/Internal/LazyInternalConnection.cs:118 
at System.Data.Entity.Internal.LazyInternalContext.get_ProviderName() [0x00000] in /private/tmp/source/bockbuild/profiles/mono-mac-release/build-root/mono-2.11/_build/mono-2.11.git/external/entityframework/src/EntityFramework/Internal/LazyInternalContext.cs:290 
at System.Data.Entity.Internal.DefaultModelCacheKeyFactory.Create (System.Data.Entity.DbContext) [0x00022] in /private/tmp/source/bockbuild/profiles/mono-mac-release/build-root/mono-2.11/_build/mono-2.11.git/external/entityframework/src/EntityFramework/Internal/DefaultModelCacheKeyFactory.cs:20 
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() [0x00091] in /private/tmp/source/bockbuild/profiles/mono-mac-release/build-root/mono-2.11/_build/mono-2.11.git/external/entityframework/src/EntityFramework/Internal/LazyInternalContext.cs:388 
at System.Data.Entity.Internal.InternalContext.Initialize() [0x00000] in /private/tmp/source/bockbuild/profiles/mono-mac-release/build-root/mono-2.11/_build/mono-2.11.git/external/entityframework/src/EntityFramework/Internal/InternalContext.cs:421 
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType (System.Type) [0x00000] in /private/tmp/source/bockbuild/profiles/mono-mac-release/build-root/mono-2.11/_build/mono-2.11.git/external/entityframework/src/EntityFramework/Internal/InternalContext.cs:650 
at System.Data.Entity.Internal.Linq.InternalSet`1<Web.Users>.Initialize() <0x0006f> 
at System.Data.Entity.Internal.Linq.InternalSet`1<Web.Users>.get_InternalContext() <0x00028> 
at System.Data.Entity.Internal.Linq.InternalSet`1<Web.Users>.Find (object[]) <0x0002e> 
at System.Data.Entity.DbSet`1<Web.Users>.Find (object[]) <0x0002f> 
at Web.Controllers.HomeController.Index() [0x0001c] in /Users/nix/Documents/Projects/Dot.NET/Store/Web/Controllers/HomeController.cs:15 
at (wrapper dynamic-method) object.lambda_method (System.Runtime.CompilerServices.Closure,System.Web.Mvc.ControllerBase,object[]) <IL 0x00006, 0x0003f> 
at System.Web.Mvc.ActionMethodDispatcher.Execute (System.Web.Mvc.ControllerBase,object[]) [0x00000] in /private/tmp/source/bockbuild/profiles/mono-mac-release/build-root/mono-2.11/_build/mono-2.11.git/mcs/class/System.Web.Mvc3/Mvc/ActionMethodDispatcher.cs:27 
at System.Web.Mvc.ReflectedActionDescriptor.Execute (System.Web.Mvc.ControllerContext,System.Collections.Generic.IDictionary`2<string, object>) [0x00074] in /private/tmp/source/bockbuild/profiles/mono-mac-release/build-root/mono-2.11/_build/mono-2.11.git/mcs/class/System.Web.Mvc3/Mvc/ReflectedActionDescriptor.cs:84 
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod (System.Web.Mvc.ControllerContext,System.Web.Mvc.ActionDescriptor,System.Collections.Generic.IDictionary`2<string, object>) [0x00000] in /private/tmp/source/bockbuild/profiles/mono-mac-release/build-root/mono-2.11/_build/mono-2.11.git/mcs/class/System.Web.Mvc3/Mvc/ControllerActionInvoker.cs:173 
at System.Web.Mvc.ControllerActionInvoker/<InvokeActionMethodWithFilters>c__AnonStorey3B.<>m__46() [0x00014] in /private/tmp/source/bockbuild/profiles/mono-mac-release/build-root/mono-2.11/_build/mono-2.11.git/mcs/class/System.Web.Mvc3/Mvc/ControllerActionInvoker.cs:216 
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter (System.Web.Mvc.IActionFilter,System.Web.Mvc.ActionExecutingContext,System.Func`1<System.Web.Mvc.ActionExecutedContext>) [0x00033] in /private/tmp/source/bockbuild/profiles/mono-mac-release/build-root/mono-2.11/_build/mono-2.11.git/mcs/class/System.Web.Mvc3/Mvc/ControllerActionInvoker.cs:189 

有什麼建議或線索有關正在發生什麼?

+0

看起來像單聲道的錯誤。不幸的是,由於沒有涉及類型的信息,所以異常消息不是很有幫助。另一方面,堆棧跟蹤將指向文件和文件中的位置... – Pawel

+0

您是否引用了Npgsql的第三方dll?如果你是,你不需要它,嘗試刪除參考和DLL,並用Npgsq – startupsmith

回答

2

兩個步驟來解決:

1)確保帶有單默認npgsql庫在引用引用和一個不存在的特定位置不是一個指向。

2)將文件輸出爲Npgsql的版本,該文件要在項目的bin目錄中使用。當你去運行你的項目或測試時它會自動使用這個。

+0

這工作。現在我正在使用Nuget包管理器構建到xamarin工作室,所以我不再會遇到問題了。 – nixdaemon

1

根據我的經驗,這個問題是由於編譯器在dp和gp版本的Npgsql之間混淆造成的。

你是否引用了Npgsql的dll? 如果你是,你不需要它,嘗試刪除引用和dll,並用單聲道的Npgsql包替換它。

如果你確實需要Npgsql的dll,那麼你需要從你的項目中刪除所有對它的引用,把它替換成gac。 您可以通過轉到Npgsql dll的目錄並用您的Npgsql dll替換它來快速執行此操作。 目錄的位置將取決於您的操作系統。

  • 爲Ubuntu它位於:/ usr/lib中/單聲道/ GAC/Npgsql的
  • 對於Mac它位於:/Library/Frameworks/Mono.framework/Home/lib/mono/gac/ Npgsql

請注意,通過這樣做,您的Npgsql dll可能會在您下次升級Mono時被替換。

+0

替換它,我使用mono提供的Npgsql。我會嘗試使用外部Npgsql DLL,並會讓你們發佈。僅供參考,我使用Mac進行開發。 – nixdaemon

+0

我有同樣的問題,我已經從我的應用程序中刪除了所有引用(它是MVC3 + EF CodeFirst)。來自GAC的Npgsql.dll正在被加載(可以在MonoDevelop的輸出中看到,還增加了DbProvider的東西..其他想法? – Martin

相關問題