0

I have two databases that I need to call data from. IntranetApps and LawOfficerServer (LORS).

The IntranetApps portion was written and is working. I'm adding the LawOfficerServer portion.

I created and EDMX (LORSDataEntities.edmx) and two .tt files, LORSDataEntities.Context.tt and LORSDataEntities.tt patterned after what was working for IntranetApps. I created a separate Repository class, LORSRepository.cs patterned after what was working.

All the Repository, EDMX, and .tt files are in a project called DataAccess.

I call for the data in the ServicesLayer project, which has my LORSDataService.cs (patterned after what is working in the other side) where I have a basic call to FindAll().

Here's the code:

    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;
            }
        }
    }

Incident is a table in the EDMX. However I get this message:

The entity type Incident is not part of the model for the current context.

Using SQL Server Profiler, I can see the IntranetApps calls, but not the LORS calls. It's as if the repository is never trying to talk to the database.

In the web.config for the Web application I have this 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=&quot;data source=FBHBGSQLDEV01;initial catalog=LawOfficerServer;integrated security=False;User Id=redacted;Password=xxxxxxx;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

Any thoughts where I went wrong, or to troubleshoot what I missed?

Here is the beginning of the LORSRepository class:

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);
    }

And here is my IUnitOfWork class:

public interface IUnitOfWork : IDisposable
{
    void Save();

}
4

1 回答 1

0

我遇到的问题是 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=&quot;data source=FBHBGSQLDEV01;initial catalog=LawOfficerServer;integrated security=False;User Id=redacted;Password=xxxxxxx;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
<!--Models.IntranetAppsEntities-->

我应该有这个:

<add name="LORSDataEntities" connectionString="metadata=res://*/Models.LORS.LORSDataEntities.csdl|res://*/Models.LORS.LORSDataEntities.ssdl|res://*/Models.LORS.LORSDataEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=FBHBGSQLDEV01;initial catalog=LawOfficerServer;integrated security=False;User Id=redacted;Password=xxxxxxx;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
<!--Models.LORS.LORSDataEntities-->

这是一个复制粘贴问题,加上我的经验不足。希望这可以帮助别人。

于 2013-08-27T17:30:22.263 回答