Entity framework 伪造实体框架并尝试LINQ查询它会产生错误

Entity framework 伪造实体框架并尝试LINQ查询它会产生错误,entity-framework,unit-testing,mocking,repository-pattern,Entity Framework,Unit Testing,Mocking,Repository Pattern,我们一直在试图找出如何伪造DbContext。所以我们在这里遵循了这个例子 代码(一个用于EF上的抽象层(错误如下)和一个直接使用EF(工作正常) 我能想到的是,这可能与预先生成的EF对象之上的抽象对象有关。EF对象没有表/实体之间关系的概念??有什么想法吗 我们一直在试图找到一种方法,为我们的伪代码和真实代码找到一条共同的路径。也许简单的答案是停止这样做,只是对存储库进行模拟调用,然后在此之后进行单元测试 CongressContext context = new CongressC

我们一直在试图找出如何伪造DbContext。所以我们在这里遵循了这个例子

代码(一个用于EF上的抽象层(错误如下)和一个直接使用EF(工作正常)

我能想到的是,这可能与预先生成的EF对象之上的抽象对象有关。EF对象没有表/实体之间关系的概念??有什么想法吗

我们一直在试图找到一种方法,为我们的伪代码和真实代码找到一条共同的路径。也许简单的答案是停止这样做,只是对存储库进行模拟调用,然后在此之后进行单元测试

    CongressContext context = new CongressContext();
        var repo = new CongressRepository(context);

    //    IEnumerable<tMember> members = repo.Get100MembersDirectEF();
        IEnumerable<tMember> members = repo.Get100Members();
CongressContext上下文=新CongressContext();
var repo=新的CongressRepository(上下文);
//IEnumerable members=repo.Get100MembersDirectEF();
IEnumerable members=repo.Get100Members();

 public class CongressRepository : ICongressRepository
{
    ICongressContext db;
    CongressDb_DevEntities realDb = new CongressDb_DevEntities();

    public CongressRepository()
    {
        this.db = new CongressContext();
    }
    public CongressRepository(ICongressContext context)
    {
        this.db = context;
    }

    public List<tMember> Get100Members()
    {
        var members = db.Members.Where(x => x.MembersID < 100).ToList();
        return members;
    }

    public List<tMember> Get100MembersDirectEF()
    {
        realDb.Configuration.LazyLoadingEnabled = false;
        var members = realDb.tMembers.Where(x => x.MembersID < 100).ToList();
        return members;
    }





{"One or more validation errors were detected during model generation:\r\n\r\nCongress.Data.tEducationType: : EntityType 'tEducationType' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tCongress: : EntityType 'tCongress' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tCongressMemPositionReason: : EntityType 'tCongressMemPositionReason' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tReasonType: : EntityType 'tReasonType' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tDistrict: : EntityType 'tDistrict' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tParty: : EntityType 'tParty' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tPosition: : EntityType 'tPosition' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tChamber: : EntityType 'tChamber' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tState: : EntityType 'tState' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tMemMilitary: : EntityType 'tMemMilitary' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tMemOccupation: : EntityType 'tMemOccupation' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tOccupationType: : EntityType 'tOccupationType' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tInterestGroup: : EntityType 'tInterestGroup' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tRaceType: : EntityType 'tRaceType' has no key defined. Define the key for this EntityType.\r\nCongress.Data.tReligion: : EntityType 'tReligion' has no key defined. Define the key for this EntityType.\r\ntEducationTypes: EntityType: EntitySet 'tEducationTypes' is based on type 'tEducationType' that has no keys defined.\r\ntCongresses: EntityType: EntitySet 'tCongresses' is based on type 'tCongress' that has no keys defined.\r\ntCongressMemPositionReasons: EntityType: EntitySet 'tCongressMemPositionReasons' is based on type 'tCongressMemPositionReason' that has no keys defined.\r\ntReasonTypes: EntityType: EntitySet 'tReasonTypes' is based on type 'tReasonType' that has no keys defined.\r\ntDistricts: EntityType: EntitySet 'tDistricts' is based on type 'tDistrict' that has no keys defined.\r\ntParties: EntityType: EntitySet 'tParties' is based on type 'tParty' that has no keys defined.\r\ntPositions: EntityType: EntitySet 'tPositions' is based on type 'tPosition' that has no keys defined.\r\ntChambers: EntityType: EntitySet 'tChambers' is based on type 'tChamber' that has no keys defined.\r\ntStates: EntityType: EntitySet 'tStates' is based on type 'tState' that has no keys defined.\r\ntMemMilitaries: EntityType: EntitySet 'tMemMilitaries' is based on type 'tMemMilitary' that has no keys defined.\r\ntMemOccupations: EntityType: EntitySet 'tMemOccupations' is based on type 'tMemOccupation' that has no keys defined.\r\ntOccupationTypes: EntityType: EntitySet 'tOccupationTypes' is based on type 'tOccupationType' that has no keys defined.\r\ntInterestGroups: EntityType: EntitySet 'tInterestGroups' is based on type 'tInterestGroup' that has no keys defined.\r\ntRaceTypes: EntityType: EntitySet 'tRaceTypes' is based on type 'tRaceType' that has no keys defined.\r\ntReligions: EntityType: EntitySet 'tReligions' is based on type 'tReligion' that has no keys defined.\r\n"}
公共类CongressRepository:ICongressRepository
{
ICongressContext数据库;
CongressDb_-DevEntities realDb=新的CongressDb_-DevEntities();
public CongressRepository()
{
this.db=新的CongressContext();
}
public CongressRepository(ICongressContext上下文)
{
this.db=上下文;
}
公共列表Get100Members()
{
var members=db.members.Where(x=>x.MembersID<100).ToList();
返回成员;
}
公共列表Get100MembersDirectEF()
{
realDb.Configuration.LazyLoadingEnabled=false;
var members=realDb.tMembers.Where(x=>x.MembersID<100.ToList();
返回成员;
}
{"在模型生成过程中检测到一个或多个验证错误:\r\n\r\nCongress.Data.tEducationType::EntityType“tEducationType”未定义键。请定义此EntityType的键。\r\nCongress.Data.tCongress::EntityType“tCongress”未定义键。请定义此EntityType的键。\r\nCongress.Data.tCongressMemPositionReason::EEntityType“tCongressMemPositionReason”未定义键。请定义此EntityType的键。\r\nCongress.Data.tReasonType::EntityType“tReasonType”未定义键。请定义此EntityType的键。\r\nCongress.Data.Tdirect::EntityType“Tdirect”未定义键。请定义此EntityType的键。\r\nCongress.Data.tParty::EntitY类型“tParty”没有定义键。请定义此EntityType的键。\r\nCongress.Data.tPosition::EntityType“tPosition”没有定义键。请定义此EntityType的键。\r\nCongress.Data.tChamber::EntityType“tChamber”没有定义键。请定义此EntityType的键。\r\nCongress.Data.tState::EntityType“tState”没有定义键ned.定义此EntityType的键。\r\nCongress.Data.tMemMilitary::EntityType“tMemMilitary”未定义键。定义此EntityType的键。\r\nCongress.Data.tmemoccuption::EntityType“tmemoccuption”未定义键。定义此EntityType的键。\r\nCongress.Data.tOccupationType::EntityType“tOccupationType”未定义键y已定义。定义此EntityType的键。\r\nCongress.Data.tInterestGroup::EntityType“tInterestGroup”未定义键。定义此EntityType的键。\r\nCongress.Data.tRaceType::EntityType“tRaceType”未定义键。定义此EntityType的键。\r\nCongress.Data.tReligion::EntityType“tReligion”未定义键。D定义此EntityType的键。\r\n教育类型:EntityType:EntitySet“tEducationTypes”基于未定义键的类型“tEducationType”。\r\n教育类型:EntityType:EntitySet“tCongress”基于未定义键的类型“tCongress”。\r\n国会议员职位原因:EntityType:EntitySet“TcongressMemPositionReasions”是基于未定义键的类型“tCongressMemPositionReason”。\r\ntReasonTypes:EntityType:EntitySet“tReasonTypes”基于未定义键的类型“tReasonType”。\r\nDistricts:EntityType:EntitySet“tDistricts”基于未定义键的类型“tDistrict”。\r\nParties:EntityType:EntitySet“tParties”基于类型t未定义键的“参与方”。\r\nPositions:EntityType:EntitySet“tPositions”基于未定义键的类型“tPosition”。\r\nChambers:EntityType:EntitySet“tChambers”基于未定义键的类型“tChamber”。\r\nState:EntityType:EntitySet“tStates”基于未定义键的类型“tState”。\r\naries:EntityType:EntitySet'tMemMilitaries'基于未定义键的类型'tMemMilitary'。\r\nMemocations:EntityType:EntitySet'tMemocations'基于未定义键的类型'tMemocations'。\r\nEntityTypes:EntitySet'tOccupationTypes'基于未定义键的类型'tOccupationType'\EntiteRestGroups:EntityType:EntitySet“tInterestGroups”基于未定义键的类型“tInterestGroup”。\r\ntRaceTypes:EntityType:EntitySet“tRaceType”基于未定义键的类型“tRaceType”。\r\n区域:EntityType:EntitySet“tReligions”基于未定义键的类型“tReligion”。\r\n“}

这样试试,不要担心伪造上下文,而是伪造存储库

真正的存储库如下所示:

public class CongressRepository : ICongressRepository
{
    ICongressContext db;
    CongressDb_DevEntities realDb = new CongressDb_DevEntities();

    public CongressRepository()
    {
        this.db = new CongressContext();
    }
    public CongressRepository(ICongressContext context)
    {
        this.db = context;
    }

    public List<tMember> GetMembers()
    {
        realDb.Configuration.LazyLoadingEnabled = false;
        var members = realDb.tMembers.Where(x => x.MembersID < 100).ToList();
        return members;
    }
}
public class MockCongressRepository : ICongressRepository
{
    public List<tMember> GetMembers()
    {
         var members = new List<tMembers>{ new tMember { id = 1 } };
         return members;
    }
}
var controller = new CongressController(new MockCongressRepository());
使用Moq的单元测试:

    var mockCongressRepository = new Mock<ICongressRepository>();
    mockCongressReposiory.Setup(mcr => mcr.GetMembers())
                         .Returns(new List<tMembers>{ new tMember { id = 1 } });        
    var controller = new CongressController(mockCongressRepository.Object);
var mockCongressRepository=new Mock();
mockCongressReposiory.Setup(mcr=>mcr.GetMembers())
.Returns(新列表{new tMember{id=1}});
var controller=新的CongressController(mockCongressRepository.Object);

注意:这里我们去掉了MockCongressRepository类,mock是在单元测试附近定义的。

是的。我认为这更像是标准方法。但我认为其缺点是不能对linq查询进行单元测试……但是因为linq2Entity查询实际上是集成测试,而不是单元测试我无论如何都不应该尝试这样做,所以是的,现在我们将尝试这样做