Entity Framework - Repository Pattern を使用しようとしています。(Asp.net C#、EF4)
各 DB テーブルのリポジトリを作成します。しかし、テーブルを結合すると、エラーが発生します
「指定された LINQ 式には、さまざまなコンテキストに関連付けられているクエリへの参照が含まれています。」
エラーメッセージを回避するために、次のようにすべてを1つのクラスに入れました:
public class WebOrderRepository
{
private DbEntities context = new DbEntities(); //Web.config <add name="DBEntities" connectionString=" ...
public IQueryable<WEBORDERHD> WebOrderHds
{
get { return context.WEBORDERHDs; }
}
public IQueryable<WEBORDERLN> WebOrderLns
{
get { return context.WEBORDERLNs; }
}
}
私のコードを見直してください。
これは私のリポジトリクラスです。
public class Repository : IDisposable
{
protected ShkAdsEntities context;
private bool _disposed;
public Repository()
{
context = new ShkAdsEntities();
}
public void Dispose() //If define this class as Static then, 'Dispose': cannot declare instance members in a static class
{
DisposeObject(true);
GC.SuppressFinalize(this);
}
~Repository()
{
DisposeObject(false);
}
private void DisposeObject(bool disposing)
{
if (_disposed)
{
return;
}
if(disposing){
if (context != null)
{
context.Dispose();
}
_disposed = true;
}
}
}
public class WebOrderHdRepository : Repository
{
public IQueryable<WEBORDERHD> WebOrderHds
{
get { return context.WEBORDERHDs; }
}
public void Create(WEBORDERHD obj)
{
}
public void Delete(WEBORDERHD ojb)
{
}
public void SubmitChanges()
{
context.SaveChanges();
}
}
public class WebOrderLnRepository : Repository
{
public IQueryable<WEBORDERLN> WebOrderLns
{
get { return context.WEBORDERLNs; }
}
public void Create(WEBORDERLN obj)
{
}
public void Delete(WEBORDERLN ojb)
{
}
public void SubmitChanges()
{
context.SaveChanges();
}
}
これがテスト用のコントローラーです
[HttpGet]
public ActionResult repositoryTest()
{
WebOrderHdRepository webOrderHdRepository = new WebOrderHdRepository();
WebOrderLnRepository webOrderLnRepository = new WebOrderLnRepository();
var result = (from x in webOrderHdRepository.WebOrderHds
join u in webOrderLnRepository.WebOrderLns on x.OrderNo equals u.OrderNo
select new {x.OrderNo}).SingleOrDefault();
return Content(result.OrderNo);
}
コンテキストを静的として定義しようとしましたが、
protected static ShkAdsEntities context = null;
public Repository()
{
if (context == null)
{
context = new ShkAdsEntities();
}
}
その後、別のエラーが発生し、
Sequence contains more than one element
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Sequence contains more than one element
Source Error:
Line 116: {
Line 117: WebOrderHdRepository webOrderHdRepository = new WebOrderHdRepository();
Line 118: WebOrderLnRepository webOrderLnRepository = new WebOrderLnRepository(); <== ERROR POINT
Line 119:
Line 120: var result = (from x in webOrderHdRepository.WebOrderHds
Entity Framework-Repository パターンを多く検索します。しかし、ほとんどのことは非常に複雑です。だから私は上記のように簡単にしたい。
アドバイスお願いします〜
ありがとう!
[編集]
私はこれを試みます、
using(WebOrderHdRepository webOrderHdRepository = new WebOrderHdRepository())
using (WebOrderLnRepository webOrderLnRepository = new WebOrderLnRepository())
{
.
.
エラーが発生しますが、
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ObjectDisposedException: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
Source Error:
Line 114: {
Line 115: using(WebOrderHdRepository webOrderHdRepository = new WebOrderHdRepository())
Line 116: using (WebOrderLnRepository webOrderLnRepository = new WebOrderLnRepository())
Line 117: {
Line 118:
時間を2回処分しようとしていると思いますが、コードを修正する方法がわかりません...
どなたかご存知の方教えてください〜
ありがとう