2011-06-24 27 views
0

我试图用假货的EF 4.1 DataContext的测试库,而不会测试数据库(由于部署问题)C#继承和接口混乱

我做这样的事情

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

它的伟大工程的大部分时间,但我的问题是,有时在我的代码我使用的东西,如:

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

,并抱怨说,

“IEmployeeContext”不包含定义“进入”

我似乎无法理解我需要的模式改变,让我获得了context.Entry和context.Database部分

+0

你可以显示“context”声明的代码吗? – CesarGon

+0

你如何申报上下文? – Alan

+0

这是铸造相关的问题吗? 'IEmployeeContext'没有定义'Entry'属性/函数,而是定义在'DbContext'类中。 '((IEmployeeContext)context).Entry(department).State ..'可能会解决你的问题。 –

回答

1

您得到该特定错误的原因是因为IEmployeeContext不包含名为Entry的方法。

EntryDbContext的成员。

+0

如果我使IEmployeeContext成为Entry方法的一部分,那么我需要在它中实现它EmployeeContext和FakeEmployeeContext,它已经在EmployeeContext中实现了 – MarkKGreenway

+0

对,该方法来自'DbContext'。一个可能的解决方案是让你的虚假类继承'DbContext',尽管我没有用EF做足够的事情来告诉你它是否对你有用。 –

+0

只是做了一些挖掘,最糟糕的情况是,您可以隐藏DbContext中的'Entry'方法以用于您的假员工环境。我相信它应该是这样的:'public new DbEntityEntry Entry (TEntity entity)where TEntity:class {}' –