我收到了“System.InvalidCastException” 无法从源类型转换为目标类型。“尝试使用mono 4.5配置文件和后端postgres从asp.net mvc3访问数据库时。
以下是我的代码:
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 :(得分:2)
要修复的两个步骤:
1)确保引用中引用了mono的默认npgsql
库,而不是指向不存在的特定位置的库。
2)将您要使用的Npgsql.dll
版本的Npgsql
文件扔到项目的bin目录中。当你去运行你的项目或测试时,它会自动使用这个。
答案 1 :(得分:1)
根据我的经验,这个问题是由编译器在Npgsql的dll和gac版本之间混淆引起的。
您是否引用了一个Npgsql的DLL? 如果您是并且您不需要它,请尝试删除引用和dll并将其替换为单声道的Npgsql包。
如果确实需要Npgsql的dll,那么您需要从项目中删除对它的所有引用,将其替换为gac。 您可以通过转到Npgsql dll的目录并将其替换为Npgsql dll来快速完成此操作。 目录的位置取决于您的操作系统。
请注意,通过执行此操作,下次升级Mono时可能会替换Npgsql dll。