在ASP.NET Controller中访问上下文的问题

时间:2018-11-21 19:41:49

标签: c# asp.net asp.net-mvc entity-framework

我正在使用ASP.NET MVC构建一个Web应用程序。我也打算访问一个远程MySQL数据库。想保留实体框架。

我想做的是访问控制器中的上下文。使用vanilla数据库解决方案可以毫无问题地工作。但是,既然我正在访问远程数据库,它似乎无法正常工作。与数据库的连接是有效的-它实际上可以正确迁移和播种。但是,当我尝试访问上下文成员时,该应用程序将失败。

Web.config

<connectionStrings>
    <remove name="DBName" />
    <add name="DBName" connectionString="server=1.2.3.4;port=1234;user id=DBAdmin;password=DBPass;database=DBName;integrated security=True" providerName="MySQL.Data.MySqlClient" />
</connectionStrings>
<system.data>
    <DbProviderFactories>
        <remove invariant="MySql.Data.MySqlClient" />
        <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.9, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
</system.data>
<entityFramework>
    <defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6">
        <parameters>
            <parameter value="v11.0" />
        </parameters>
    </defaultConnectionFactory>
    <providers>
        <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.9, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
</entityFramework>

Context.cs

[DbConfigurationType (typeof (MySql.Data.Entity.MySqlEFConfiguration))]
public class Context : DbContext {
    public Context () : base ("name=DBName") {
        Database.SetInitializer (new MigrateDatabaseToLatestVersion <Context, System.Data.Entity.Migrations.DbMigrationsConfiguration <Context>> ());
    }

    public DbSet<Software> Softwares { get; set; }
}

SoftwareController

public class SoftwareController : Controller {
    public ActionResult Index() {
        using (Context context = new Context ()) {
            List<Software> softwares = context.Softwares.ToList ();

            return View (softwares);
        }
    }
}

错误

  

字典中不存在给定的键。

源错误

  

列出软件= context.Softwares.ToList();

堆栈跟踪

  

[KeyNotFoundException:字典中不存在给定的键。]      System.Collections.Generic.Dictionary`2.get_Item(TKey键)+12769097      MySql.Data.MySqlClient。<> c。<。cctor> b__2_7(MySqlConnectionStringBuilder msb,MySqlConnectionStringOption发送者)+16      MySql.Data.MySqlClient.MySqlConnectionStringBuilder.get_Item(字符串关键字)+28      MySql.Data.MySqlClient.MySqlConnectionStringBuilder.GetConnectionString(布尔值includePass)+298      MySql.Data.MySqlClient.MySqlConnection.get_ConnectionString()+41      System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.b__12(DbConnection t,DbConnectionInterceptionContext`1 c)+10      System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget目标,Func`3操作,TInterceptionContext拦截上下文,Action`3执行,Action`3执行)+72      System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.GetConnectionString(DbConnection连接,DbInterceptionContext拦截上下文)+361      System.Data.Entity.Internal.InternalConnection.GetStoreConnectionString(DbConnection连接)+159      System.Data.Entity.Internal.InternalConnection.OnConnectionInitialized()+28      System.Data.Entity.Internal.EagerInternalConnection..ctor(DbContext上下文,DbConnection existingConnection,布尔连接拥有)+70      System.Data.Entity.DbContext..ctor(DbConnection现有连接,布尔contextOwnsConnection)+55      System.Data.Entity.Migrations.History.HistoryContext..ctor(DbConnection existingConnection,字符串defaultSchema)+16      MySql.Data.Entity。<> c。<。ctor> b__0_1(DbConnection existingConnection,字符串defaultSchema)+29      System.Data.Entity.Migrations.History.HistoryRepository.CreateContext(DbConnection连接,字符串模式)+37      System.Data.Entity.Migrations.History.d__16.MoveNext()+737      System.Linq.Enumerable.Any(IEnumerable`1来源)+77      System.Data.Entity.Migrations.DbMigrator.UpdateInternal(字符串targetMigration)+58      System.Data.Entity.Migrations。<> c__DisplayClasse.b__d()+13      System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(动作mustSucceedToKeepDatabase)+422      System.Data.Entity.Migrations.DbMigrator.Update(字符串targetMigration)+78      System.Data.Entity.MigrateDatabaseToLatestVersion`2.InitializeDatabase(TContext上下文)+108      System.Data.Entity.Internal。<> c__DisplayClassf`1.b__e()+76      System.Data.Entity.Internal.InternalContext.PerformInitializationAction(动作)+60      System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()+357      System.Data.Entity.Internal.LazyInternalContext.b__4(InternalContext c)+7      System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput输入)+110      System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(动作1动作)+198      System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()+73      System.Data.Entity.Internal.InternalContext.Initialize()+30      System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)+16      System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()+53      System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()+15      System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator()+53      System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)+375      System.Linq.Enumerable.ToList(IEnumerable`1源)+54      D:\ Visual Studio \ Projects \ Website Projects \ ProtoSim \ ProtoSim \ Controllers \ SoftwareController.cs中的ProtoSim.Controllers.SoftwareController.Index():13      lambda_method(Closure,ControllerBase,Object [])+61      System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,Object []参数)+14      System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary`2参数)+157      System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary`2参数)+27      System.Web.Mvc.Async。<> c.b__9_0(IAsyncResult asyncResult,ActionInvocation innerInvokeState)+22      System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)+29      System.Web.Mvc.Async.WrappedAsyncResultBase`1.End()+49      System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)+32      System.Web.Mvc.Async.AsyncInvocationWithFilters.b__11_0()+50      System.Web.Mvc.Async。<> c__DisplayClass11_1.b__2()+228      System.Web.Mvc.Async。<> c__DisplayClass7_0.b__1(IAsyncResult asyncResult)+10      System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult)+10      System.Web.Mvc.Async.WrappedAsyncResultBase`1.End()+49      System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)+34      System.Web.Mvc.Async。<> c__DisplayClass3_6.b__3()+35      System.Web.Mvc.Async。<> c__DisplayClass3_1.b__5(IAsyncResult asyncResult)+100      System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult)+10      System.Web.Mvc.Async.WrappedAsyncResultBase`1.End()+49      System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)+27      System.Web.Mvc。<> c.b__152_1(IAsyncResult asyncResult,ExecuteCoreState innerState)+11      System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)+29      System.Web.Mvc.Async.WrappedAsyncResultBase`1.End()+49      System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)+45      System.Web.Mvc。<> c.b__151_2(IAsyncResult asyncResult,控制器控制器)+13      System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)+22      System.Web.Mvc.Async.WrappedAsyncResultBase`1.End()+49      System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)+26      System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)+10      System.Web.Mvc。<> c.b__20_1(IAsyncResult asyncResult,ProcessRequestState innerState)+28      System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)+29      System.Web.Mvc.Async.WrappedAsyncResultBase`1.End()+49      System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)+28      System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult结果)+9      System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+576      System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep步骤)+132      System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值并已完成同步)+163

0 个答案:

没有答案