views:

746

answers:

1

Hi,

I'm trying to implement the repository pattern using a generic repository like the one found here: Implementing Repository Pattern With Entity Framework

I've created a test suite (I'm using NUnit) to test the repository but I've been having issues. Here's the error I'm getting:

MyBusiness.Test.Domain.RepositoryTest.SelectCol:
System.Data.EntitySqlException : 'ColCoordinate' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly., near escaped identifier.

I believe it's coming from this method:

public ObjectQuery<E> DoQuery()
{
    return _ctx.CreateQuery<E>("[" + typeof(E).Name + "]");
}

I've made sure to add the reference at the top of the page:

using MyBusiness.Domain.DomainModel.EntityFramework;

Here's my test setup and test method:

    Repository<ColCoordinate, ObjectContext> _colRepository = null;
    ObjectContext _context = null;
    EntityContextCreator contextCreator = null;

    [SetUp]
    public void SetUp()
    {
        contextCreator = new EntityContextCreator();

        _context = contextCreator.Create();
        _colRepository = new Repository<ColCoordinate, ObjectContext>(_context);
    }

    [Test]
    public void SelectCol()
    {
        IList<ColCoordinate> colList = _colRepository.SelectAll();
        Assert.True(colList.Count > 0);
    }

Anyone know why I'm getting this error or have any suggestions as to how to fix it?

If you need more information please ask and I'll update the question.

Thanks,
Matt

+2  A: 

Well the code you identified:

public ObjectQuery<E> DoQuery(){    
      return _ctx.CreateQuery<E>("[" + typeof(E).Name + "]");
}

Is very suspect, the TypeName is not what is required, you need the EntitySet name, sometimes they are the same thing, but often they are not, especially if you've manually updated your model to have more readable properties.

For example if you have:

ctx.Customers which returns a type called Customer, the typename is Customer the EntitySet name is Customers.

Tip 13 in my series of tips has some code that shows you how to get the EntitySet name from a Type.

Hope this helps

Alex

Alex James