Mono 4.5配置文件,实体框架Npgsql-NpgsqlSystem.InvalidCastException无法从源类型转换为目标类型 [英] Mono 4.5 profile, entity framework Npgsql - NpgsqlSystem.InvalidCastException Cannot cast from source type to destination type

查看:78
本文介绍了Mono 4.5配置文件,实体框架Npgsql-NpgsqlSystem.InvalidCastException无法从源类型转换为目标类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了"System.InvalidCastException 无法从源类型转换为目标类型."当尝试在后端使用mono 4.5配置文件和postgres从asp.net mvc3访问数据库时.

I have getting "System.InvalidCastException Cannot cast from source type to destination type." when trying to access the database from asp.net mvc3 using mono 4.5 profile and postgres at the backend.

这是我的代码的样子:

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:

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

有什么建议或线索吗?

推荐答案

要修复的两个步骤:

1)确保引用中引用了Mono随附的默认npgsql库,而不是一个指向不存在的特定位置的库.

1) Make sure the default npgsql library that comes with mono is referenced in your references and not one pointing at a specific location that doesn't exist.

2)将Npgsql.dll文件作为要在项目的bin目录中使用的Npgsql版本.当您运行项目或测试时,它将自动使用这一功能.

2) Throw the Npgsql.dll file for the version of Npgsql that you want to use in the bin directory for your project. It will automatically use this one when you go to run your project or tests.

这篇关于Mono 4.5配置文件,实体框架Npgsql-NpgsqlSystem.InvalidCastException无法从源类型转换为目标类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆