这会让我发疯。
我构建了一个用于 Intranet 的简单网络应用程序,它在本地测试期间运行良好。当我将它部署到 IIS 服务器上时,它给我带来了问题,我们认为这与错误的 web.config 文件有关。
我完全按照 MySQL 文档所述仔细检查并配置了 web.config 文件,现在该应用程序也无法在本地运行。这显然是相关的,但我无法弄清楚是什么原因造成的。
使用 MySQL 设置,我得到一个 NullReferenceException 和这个堆栈跟踪:
[NullReferenceException: Object reference not set to an instance of an object.]
MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(DbConnection connection) +56
System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +234
MySql.Data.Entity.MySqlManifestTokenResolver.ResolveManifestToken(DbConnection connection) +36
System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest) +55
System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +56
System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +79
System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +112
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +635
System.Data.Entity.Internal.InternalContext.Initialize() +28
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +27
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +79
System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() +28
System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.IEnumerable.GetEnumerator() +64
System.Web.UI.WebControls.ListViewPagedDataSource.GetEnumerator() +305
System.Web.UI.WebControls.ListView.CreateItemsWithoutGroups(ListViewPagedDataSource dataSource, Boolean dataBinding, InsertItemPosition insertPosition, ArrayList keyArray) +215
System.Web.UI.WebControls.ListView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +952
System.Web.UI.WebControls.ListView.PerformDataBinding(IEnumerable data) +63
System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +128
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +34
System.Web.UI.WebControls.ModelDataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +80
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143
System.Web.UI.WebControls.ListView.PerformSelect() +114
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +114
System.Web.UI.WebControls.ListView.CreateChildControls() +77
System.Web.UI.Control.EnsureChildControls() +92
System.Web.UI.Control.PreRenderRecursiveInternal() +42
System.Web.UI.Control.PreRenderRecursiveInternal() +160
System.Web.UI.Control.PreRenderRecursiveInternal() +160
System.Web.UI.Control.PreRenderRecursiveInternal() +160
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +883
尝试逐步执行代码只会导致它在第一项运行时立即失败,似乎没有链接到任何特定行。这是我的 web.config 文件中的内容,现在我已经更改了它。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<appSettings>
<add key="PrinterTemplate" value="--redacted--" />
<add key="PrinterQueue" value="--redacted--" />
<add key="FileVault" value="--redacted--" />
<add key="AdminGroup" value="--redacted--" />
</appSettings>
<location path="Modify.aspx">
<system.web>
<authorization>
<allow roles="--redacted--" />
<deny users="*,?" />
</authorization>
</system.web>
</location>
<location path="AccessDenied.aspx">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
<connectionStrings>
<add name="DefaultConnection" connectionString="DRIVER={MySQL ODBC 3.51 Driver};Server=servername;port=3306;Database=usermanuals;UId=username;password=password" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
<authentication mode="Windows" />
<authorization>
<deny users="?" />
</authorization>
<pages>
<namespaces>
<add namespace="System.Web.Optimization" />
</namespaces>
<controls>
<add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
</controls>
</pages>
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
<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" />
</DbProviderFactories>
</system.data>
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
<providers>
<provider invariantName="MySql.Data.MySqlClient"
type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>
<provider invariantName="System.Data.SqlClient"
type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
</configuration>
我看到很多人说这与他们对连接字符串的实际使用有关,但我的并不是在实际调用数据库时触发,而是在网络应用程序运行时触发。无论如何,这就是我在代码中实际使用连接字符串的方式。我的所有用法都具有相同的结构,只是命令不同。
using (OdbcConnection connection = new OdbcConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
connection.Open();
using (OdbcCommand command = new OdbcCommand("SELECT id, title FROM manuals", connection))
using (OdbcDataReader dr = command.ExecuteReader())
{
//databindings and stuff.
}
}
最佳答案
ASP.NET 项目默认使用 Entity Framework 来管理用户。如果您不使用此功能,则可以从 App_Start
文件夹中的 Startup.Auth.cs
文件中注释掉以下三行:
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and signin manager to use a single instance per request
//app.CreatePerOwinContext(ApplicationDbContext.Create);
//app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
//app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
...
}
关于c# - 与 MySQL 连接的 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36583369/