これは私を狂わせるでしょう。MySQL接続のNullReferenceException
イントラネットで使用するための簡単なWebアプリケーションを作成しましたが、ローカルテスト中に正常に動作していました。私はそれを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>
は、私は多くの人々は、それが接続文字列の彼らの実際の使用に関連していたと言う見てきましたが、鉱山は、データベースへの実際の呼び出しでトリガされていない、しかし、すぐにWebアプリケーションの実行など。それにかかわらず、これは私のコードで実際に接続文字列を使用する方法です。すべての私の用法は同じ構造で、ちょうど異なるコマンドを持っています。
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.
}
}
あなたのプロジェクトのために、エンティティフレームワークを使用していないように見えます。あなたは ''ブロック全体をコメントアウトし、それが役立つかどうかを見ることができます。 –
cableload
しかし、データベースへの実際の呼び出しではなく、ウェブアプリケーションが実行されるとすぐに私のものが起動します。なぜコードのこの部分を投稿するのですか?アプリケーションが最初に起動したときに実行されるコードはどこですか? –
これを試行すると、新しいエラーセットが生成されます。このエラーセットは、アプリケーションが読み込まれる前にトリガーされるようです。 SQL Serverへの接続を確立する際に、ネットワーク関連またはインスタンス固有のエラーが発生しました。サーバーが見つからなかったか、アクセスできませんでした。インスタンス名が正しいこと、およびSQL Serverがリモート接続を許可するように構成されていることを確認します。 (プロバイダー:SQLネットワークインターフェイス、エラー:26 - 指定されたサーバー/インスタンスの指定エラー) 実際に接続文字列が正常に機能しました。 – nosjojo