EF 6.1.3和假DbContext

时间:2016-02-21 20:27:53

标签: c# entity-framework linq unit-testing dbcontext

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

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

        public EFEditorialRepository(PixlocateEntities db)
        {
            _db = db;
        }
        // my methods here
   }

其中PixlocateEntities - 由Entity Framework自动生成的类:

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

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

然后我实现假Db上下文:

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

问题是我无法填充DbSet对象。我得到了

  

严重级代码描述项目文件行抑制状态   错误CS0266无法隐式转换类型   'System.Linq.IQueryable'来   'System.Data.Entity.DbSet'。一个   存在显式转换(你错过了吗?   cast?)PixlocateDB C:\ www \ Pixlocate \ PixlocateDB \ FakePixlocateEntities.cs 52 Active

如何填写此背景?

0 个答案:

没有答案