1

我收到“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

关于发生了什么的任何建议或线索?

4

2 回答 2

2

两步修复:

1)确保在您的参考文献中引用了mono附带的默认npgsql库,而不是指向不存在的特定位置的库。

2)将您要使用Npgsql.dll的版本的文件放入项目的 bin 目录中。Npgsql当你去运行你的项目或测试时,它会自动使用这个。

于 2013-08-13T22:35:03.937 回答
1

根据我的经验,这个问题是由编译器在 Npgsql 的 dll 和 gac 版本之间混淆引起的。

你是在引用 Npgsql 的 dll 吗?如果您是并且不需要它,请尝试删除引用和 dll 并将其替换为单声道中的 Npgsql 包。

如果您确实需要 Npgsql 的 dll,那么您将需要从项目中删除对它的所有引用,并在 gac 中替换它。您可以通过转到 Npgsql dll 的目录并将其替换为您的 Npgsql dll 来快速执行此操作。目录的位置将取决于您的操作系统。

  • 对于 Ubuntu,它位于:/usr/lib/mono/gac/Npgsql
  • 对于 Mac,它位于:/Library/Frameworks/Mono.framework/Home/lib/mono/gac/Npgsql

请注意,通过这样做,您的 Npgsql dll 可能会在您下次升级 Mono 时被替换。

于 2012-10-25T04:39:41.393 回答