0

简单的项目,新鲜的 MVC4 剃须刀网页,新的空 mysql 数据库。得到这个例外:

System.Reflection.TargetInvocationException was unhandled by user code
Message=Exception has been thrown by the target of an invocation.
Source=mscorlib
StackTrace:
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Threading.LazyInitializer.LazyHelpers`1.ActivatorFactorySelector()
   at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory)
   at System.Threading.LazyInitializer.EnsureInitialized[T](T& target, Boolean& initialized, Object& syncLock)
   at KHSWebsite.Filters.InitializeSimpleMembershipAttribute.OnActionExecuting(ActionExecutingContext filterContext) in c:\users\alex\documents\visual studio 2010\Projects\KHSWebsite\KHSWebsite\Filters\InitializeSimpleMembershipAttribute.cs:line 21
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeActionMethodFilterAsynchronously(IActionFilter filter, ActionExecutingContext preContext, Func`1 nextInChain)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<>c__DisplayClass3b.<BeginInvokeActionMethodWithFilters>b__35()
   at  System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeActionMethodFilterAsynchronously(IActionFilter filter, ActionExecutingContext preContext, Func`1 nextInChain)
InnerException: System.InvalidOperationException
   Message=The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588
   Source=KHSWebsite
   StackTrace:
        at  KHSWebsite.Filters.InitializeSimpleMembershipAttribute.SimpleMembershipInitializer..ctor() in c:\users\alex\documents\visual studio 2010\Projects\KHSWebsite\KHSWebsite\Filters\InitializeSimpleMembershipAttribute.cs:line 45
   InnerException: System.InvalidOperationException
        Message=The connection string 'DefaultConnection' in the application's configuration file does not contain the required providerName attribute."
        Source=EntityFramework
        StackTrace:
             at  System.Data.Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting(ConnectionStringSettings appConfigConnection)
             at System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig(String name, AppConfig config)
             at System.Data.Entity.Internal.LazyInternalConnection.Initialize()
             at System.Data.Entity.Internal.LazyInternalConnection.get_ProviderName()
             at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
             at System.Data.Entity.Internal.InternalContext.CreateObjectContextForDdlOps()
             at System.Data.Entity.Database.Exists()
             at  KHSWebsite.Filters.InitializeSimpleMembershipAttribute.SimpleMembershipInitializer..ctor() in c:\users\alex\documents\visual studio 2010\Projects\KHSWebsite\KHSWebsite\Filters\InitializeSimpleMembershipAttribute.cs:line 34
         InnerException: 

这是我的 web.config:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=localhost;Initial     Catalog=KHS;Integrated       Security=SSPI;"/>
  </connectionStrings>
  <system.web>
    <authentication mode="Forms">
       <forms loginUrl="login.aspx" name=".ASPXFORMSAUTH"/>
    </authentication>
    <authorization>
      <deny users="?"/>
    </authorization>
    <membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="15">
       <providers>
         <clear/>
         <add name="SqlProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="DefaultConnection" applicationName="KHS" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" requiresUniqueEmail="true" passwordFormat="Hashed"/>
       </providers>
    </membership>
    <compilation debug="true"/>
  </system.web>
</configuration>

这已从http://msdn.microsoft.com/en-us/library/6e9y4s5t(v=vs.100).aspx获取和修改

有什么想法吗?谢谢

4

1 回答 1

0

它说您错过了连接字符串中的提供者属性。你检查过那个供应商吗?

像这样的东西应该是你的连接字符串

<add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS;Initial Catalog=ApplicationServicesDB;Integrated Security=True;MultipleActiveResultSets=True"
         providerName="MySql.Data.MySqlClient"/>

system.data此外,您必须在文件中注册 mysql web.config

<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.3.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
</system.data>
于 2013-05-18T17:51:28.160 回答