Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 环境足迹6.1.3和环境_C#_Entity Framework_Linq_Unit Testing_Dbcontext - Fatal编程技术网

C# 环境足迹6.1.3和环境

C# 环境足迹6.1.3和环境,c#,entity-framework,linq,unit-testing,dbcontext,C#,Entity Framework,Linq,Unit Testing,Dbcontext,我想在内存中创建具有稳定数据的假DbContext,它很容易控制,不需要真正的DB。 我的存储库有以下代码: public class EFEditorialRepository : PixlocateBusinessLogic.IEditorialRepository { private readonly PixlocateEntities _db; public EFEditorialRepository() {

我想在内存中创建具有稳定数据的假DbContext,它很容易控制,不需要真正的DB。 我的存储库有以下代码:

    public class EFEditorialRepository : PixlocateBusinessLogic.IEditorialRepository
    {
        private readonly PixlocateEntities _db;
        public EFEditorialRepository()
        {
            _db = new PixlocateEntities();
        }

        public EFEditorialRepository(PixlocateEntities db)
        {
            _db = db;
        }
        // my methods here
   }
其中PixlocateEntities-按实体框架自动生成的类:

public partial class PixlocateEntities : DbContext
{
    public PixlocateEntities()
        : base("name=PixlocateEntities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }
然后我实现了伪数据库上下文:

public class FakePixlocateEntities : PixlocateEntities
{
    public override DbSet<EditorialPixlocateRequest> EditorialPixlocateRequests
    {
        get
        {
            var r = new List<EditorialPixlocateRequest>();

            r.Add(
                    new PixlocateDB.EditorialPixlocateRequest() {
                        ID = 1,
                        Deadline = DateTime.Now.AddMonths(-1),
                        DateCreated = DateTime.Now.AddMonths(-2),
                        RequestTypeID = 1,
                        RequestUserID = 1,
                        SpecialInstructions = "Spec instructions",
                        RequestStatusID = 1,
                        SmartphonePhotographerResponses =
                        {
                            new PixlocateDB.SmartphonePhotographerResponse() { ID =1 , DateAdded = DateTime.Now.AddDays(-40), RequestID = 1, UserID = 2, ResponseText = "response" }
                        },
                        Address = "dfdfd",
                        AddressFound = true
                    });
            r.Add(
                    new PixlocateDB.EditorialPixlocateRequest()
                    {
                        ID = 2,
                        Deadline = DateTime.Now.AddMonths(1),
                        DateCreated = DateTime.Now.AddDays(-10),
                        RequestTypeID = 1,
                        RequestUserID = 1,
                        SpecialInstructions = "Spec instructions",
                        RequestStatusID = 1,
                        SmartphonePhotographerResponses =
                        {
                            new PixlocateDB.SmartphonePhotographerResponse() { ID =1 , DateAdded = DateTime.Now.AddDays(-4), RequestID = 1, UserID = 2, ResponseText = "response" }
                        },
                        Address = "dfdfd",
                        AddressFound = true
                    });

            return r.AsQueryable();
        }

        set
        {
            base.EditorialPixlocateRequests = value;
        }
    }
}
公共类伪造PixlocateEntities:PixlocateEntities
{
公共覆盖DbSet EditorialPixlocateRequests
{
得到
{
var r=新列表();
r、 加(
新的PixlocateDB.EditorialPixlocateRequest(){
ID=1,
截止日期=DateTime.Now.AddMonths(-1),
DateCreated=DateTime.Now.AddMonths(-2),
RequestTypeID=1,
RequestUserID=1,
SpecialInstructions=“规格说明”,
RequestStatusID=1,
智能手机响应=
{
新的PixlocateDB.smartphonephonephotorresponse(){ID=1,DateAdded=DateTime.Now.AddDays(-40),RequestID=1,UserID=2,ResponseText=“response”}
},
Address=“dfd”,
AddressFound=true
});
r、 加(
新的PixlocateDB.EditorialPixlocateRequest()
{
ID=2,
截止日期=DateTime.Now.AddMonths(1),
DateCreated=DateTime.Now.AddDays(-10),
RequestTypeID=1,
RequestUserID=1,
SpecialInstructions=“规格说明”,
RequestStatusID=1,
智能手机响应=
{
新的PixlocateDB.smartphonephonephotorresponse(){ID=1,DateAdded=DateTime.Now.AddDays(-4),RequestID=1,UserID=2,ResponseText=“response”}
},
Address=“dfd”,
AddressFound=true
});
返回r.AsQueryable();
}
设置
{
base.EditorialPixlocateRequests=值;
}
}
}
问题是我无法填充DbSet对象。我明白了

严重性代码说明项目文件行抑制状态 错误CS0266无法隐式转换类型 “System.Linq.IQueryable”到 “System.Data.Entity.DbSet”。一 存在显式转换(是否缺少 cast?)PixlocateDB C:\www\Pixlocate\PixlocateDB\fakepixlocateenties.cs 52活动


如何填充此上下文?

我根据Microsoft on提供的信息为此创建了一个包装。免责声明,这是我的代码,但基于。希望这会让你走上正确的道路。我读过那篇文章。我想在没有任何不可能的模仿对象的情况下做这件事。据我所知,没有只在内存中实现EF(不考虑EF核心,但您在帖子中提到了6.1.3)。您需要模拟EF,以便覆盖行为,并且DBSET包含您想要测试的数据。如果没有某种类型的模拟/替换框架,你真的走不了多远。出于好奇,你不想使用mock框架的原因是什么?@Igor只是想了解mock是如何工作的:)这些框架大部分都是开源的,你可以自己检查、构建、调试等等。这里有两个流行的框架(我的头上没有特别的顺序)。