データを呼び出す必要がある 2 つのデータベースがあります。IntranetApps および LawOfficerServer (LORS)。
IntranetApps 部分が作成され、機能しています。LawOfficerServer 部分を追加しています。
EDMX (LORSDataEntities.edmx) と 2 つの .tt ファイル、LORSDataEntities.Context.tt と LORSDataEntities.tt を作成し、IntranetApps で機能していたものをパターン化しました。別の Repository クラスを作成しました。LORSRepository.cs は、機能していたものをパターン化したものです。
すべてのリポジトリ、EDMX、および .tt ファイルは、DataAccess というプロジェクトにあります。
私は ServicesLayer プロジェクトでデータを呼び出します。このプロジェクトには、FindAll() への基本的な呼び出しがある LORSDataService.cs (反対側で機能しているものをパターン化したもの) があります。
コードは次のとおりです。
public static List<Incident> GetAllIncidents()
{
using (IUnitOfWork unitOfWork = new LORSDataEntities())
{
LORSRepository<DataAccess.Models.LORS.Incident> incidentRepos = new LORSRepository<DataAccess.Models.LORS.Incident>(unitOfWork);
try
{
var incidents = incidentRepos.FindAll()
.Include(i => i.Officer);
.OrderBy(i => i.IncidentYear)
.ThenBy(i => i.Officer.BadgeNumber)
.ThenBy(i => i.IncidentNumber);
return incidents.ToList();
}
catch (InvalidOperationException exc)
{
ExceptionPolicy.HandleException(exc, "DataAccess");
throw exc;
}
}
}
インシデントは EDMX のテーブルです。ただし、次のメッセージが表示されます。
The entity type Incident is not part of the model for the current context.
SQL Server Profiler を使用すると、IntranetApps の呼び出しは確認できますが、LORS の呼び出しは確認できません。リポジトリがデータベースと通信しようとしていないかのようです。
Web アプリケーションの web.config には、次の connectionString があります。
<add name="LORSDataEntities" connectionString="metadata=res://*/Models.IntranetAppsEntities.csdl|res://*/Models.IntranetAppsEntities.ssdl|res://*/Models.IntranetAppsEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=FBHBGSQLDEV01;initial catalog=LawOfficerServer;integrated security=False;User Id=redacted;Password=xxxxxxx;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
どこが間違っていたのか、または見逃したものをトラブルシューティングするために考えたことはありますか?
LORSRepository クラスの冒頭は次のとおりです。
public class LORSRepository<T> : IRepository<T> where T : class
{
private LORSDataEntities _context;
private DbSet<T> _objectSet;
public LORSRepository(IUnitOfWork dbContext)
{
if (dbContext == null)
{
throw new ArgumentNullException("A unit of work is required");
}
this._context = dbContext as LORSDataEntities;
this._objectSet = _context.Set<T>();
}
public LORSRepository()
{
this._context = new LORSDataEntities();
this._objectSet = _context.Set<T>();
}
public void Add(T entity)
{
_objectSet.Add(entity);
}
そして、ここに私の IUnitOfWork クラスがあります:
public interface IUnitOfWork : IDisposable
{
void Save();
}