これは間違いなくMultithreading issueとは異なる問題です。Unity DIを使用する場合エンティティフレームワークでNon DIサービスを追加する方法
私のすべてのサービスに対して、初期化され正しく動作していると思われるエンティティフレームワークコンテキストを使用して、依存性注入(DI)を使用するUnityを使用する。
現在mvcとUnity.WebApiとエンティティフレームワーク6.0.0.0で.net 4.5を使用しています。
可能であれば、このモデルでサービスを永続化したり、DIの外部で使用して、永続化されたサービスのエンティティフレームワークを閉じて再初期化できるかどうかを知りたいです。
現在、スタッククラスを使用してサービスを永続化しようとするとこのエラーが発生するため、パフォーマンスが非常に高速になるため、頻繁に使用しています。
The Error message I am getting is: Error Message: The underlying provider failed on Open. - The connection was not closed. The connection's current state is connecting.( at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
目的は、1つのエンティティのみを取得し、依存性を注入したバージョンと比較して非常に高速にロードし、結果をサービスを通じて公開することです。エンティティは、ログ記録の目的でSQLテーブルに関連付けられています。ここで
は私がサービスを永続化するためにやろうとしている理由のコードスニペットです:
using System;
using System.Linq;
using Davinci.Models;
namespace Services.Services
{
public interface ILogService
{
void SaveLog(UserLog log);
UserLog RetreiveLog(DateTime dateTime);
}
public class LogService : ILogService
{
private DavinciEntities _DavinciEntities;
public LogService(DavinciEntities context)
{
_DavinciEntities = context;
}
private void SaveLog(UserLog log)
{
_DavinciEntities.UserLogs.Add(log);
_DavinciEntities.SaveChanges();
}
public UserLog RetreiveLog(DateTime dateTime)
{
return _DavinciEntities.UserLogs.Where(m => m.LogTime.ToShortDateString() == dateTime.ToShortDateString()).FirstOrDefault();
}
}
public static class PersistService
{
public static UserLogService userLogService {get; set;}
public static void PersistUserLog(UserLogService service)
{
IUserLogService UserLogService;
if (UserAccessLog == null)
{
UserLogService = (UserLogService)context.Configuration.DependencyResolver.GetService(typeof(UserLogService));
}
}
}
}
いくつかのコードスニペットを投稿できますか? – ovation22
[マルチスレッドエンティティフレームワークの可能な複製:接続が閉じられませんでした。接続の現在の状態は接続しています](https://stackoverflow.com/questions/12521695/multithreadingentity-framework-the-connection-was-not-closed-the-connections) – CodeCaster
問題を再現するために必要な_最小限のコードを表示してください、[mcve]で説明されています。また、[ask]で説明されているようにあなたの研究を共有してください。このエラーは、接続が閉じられている古いDbContextインスタンスを再使用すること、適切に廃棄しないこと、またはDIコンテナ内に不正な存続期間を与えることと関連しています。これらのクラスをどのように構成し、使用するかを示します。 – CodeCaster