我收到“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
应用上下文:
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
关于发生了什么的任何建议或线索?