0

I am trying to use fakes for EF 4.1 DataContext to test the repository without testing the database ( due to a deployment issue)

I am doing something like this

public interface IEmployeeContext
{
    IDbSet<Department> Departments { get; }
    IDbSet<Employee> Employees { get; }
    int SaveChanges();
}

public class EmployeeContext : DbContext, IEmployeeContext
{
    public IDbSet<Department> Departments { get; set; }
    public IDbSet<Employee> Employees { get; set; }
}

public class FakeEmployeeContext : IEmployeeContext
{
    public IDbSet<Department> Departments { get; set; }
    public IDbSet<Employee> Employees { get; set; }
    public FakeEmployeeContext ()
    {
        Departments = new FakeDbSet<Department>();
        Employees = new FakeDbSet<Employee>();
    }
}

which works great most of the time but my problem is that sometimes in my code i use things like :

context.Entry(department).State  = EntityState.Modified;

and it complains that

'IEmployeeContext' does not contain a definition for 'Entry'

I cannot seem to comprehend what i need to change in the pattern to allow me access to the context.Entry and context.Database sections

6
  • Can you show the code for the declaration of context? Commented Jun 24, 2011 at 19:52
  • Is this a casting related problem? IEmployeeContext does not define a Entry attribute/function but instead its defined in the DbContext class. ((IEmployeeContext)context).Entry(department).State .. might solve your problem. Commented Jun 24, 2011 at 19:55
  • Seems like you want your FakeEmployeeContext to also inherit from DbContext, since EmployeeContext does Commented Jun 24, 2011 at 19:56
  • The Declaration of EmployeeContext is the second class... Commented Jun 24, 2011 at 21:11
  • Casting as IEmployeeContext wouldnt help its already resolving to that .. and Casting to EmployeeContext would defeat the purpose of a n interface Commented Jun 24, 2011 at 21:12

1 Answer 1

1

The reason you're getting that specific error is because IEmployeeContext doesn't contain a method called Entry.

Entry is a member of DbContext.

Sign up to request clarification or add additional context in comments.

3 Comments

If i make an Entry method part of the IEmployeeContext then I need to impliment it in in both EmployeeContext and FakeEmployeeContext and it is already implimented in EmployeeContext
Right, that method is coming from DbContext. A possible solution is to make your fake class inherit DbContext, although I haven't done enough with EF to be able to tell you if that will be of any use to you or not.
Just did some digging on this, and worst case, you can hide the Entry method from DbContext for the purposes of your fake employee context. It should, I believe, look like: public new DbEntityEntry<TEntity> Entry<TEntity>(TEntity entity) where TEntity: class {}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.