应用程序DbContext没有实现接口成员错误AS.NET MVC

时间:2016-11-26 10:00:42

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我意识到之前已经问过这个问题。但是我尝试了以前的解决方案,但没有一个能为我工作。我试图隔离真正的数据库,以便通过使用假的数据库上下文来运行模拟依赖性测试。我出于这个原因创建了一个ApplicationBbContext,但很难弄清楚为什么使用Application DbContext出错会导致接口成员错误

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.Identity.EntityFramework;
using MVC_ATM.Models;
using System.Data.Entity;
using MVC_ATM.ViewModels;
using MVC_ATM.Migrations;

namespace MVC_ATM.ViewModels
{
    public interface IApplicationDbContext
    {
        IDbSet<CheckingAccount> checkingAccounts { get; set; }
        IDbSet<Transaction> Transactions { get; set; }
        int SaveChanges();
    }


    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IApplicationDbContext //This is the first error
    {

        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());
            base.OnModelCreating(modelBuilder);

        }
        public IDbSet<CheckingAccount> checkAccounuts { get; set; }

        public IDbSet<Transaction> Transactions { get; set; }
    }
}
//This IApplication db is made for mock testing 
public class FakeApplicationDBContext : IApplicationDbContext //This is the second error 
{
    public IDbSet<CheckingAccount> checkAccounuts { get; set; }

    public IDbSet<Transaction> Transactions { get; set; }
    public int SaveChanges()
    {
        return 0;
    }
} 

交易控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC_ATM.ViewModels;
using MVC_ATM.Models;

namespace MVC_ATM.Controllers
{
    [Authorize]
    public class AllTransactionsController : Controller
    {
        private IApplicationDbContext DB;

        public AllTransactionsController()
        {
            DB = new ApplicationDbContext();
        }

        public AllTransactionsController(IApplicationDbContext DBContext)
        {
            DB = DBContext;
        }


    }
}

1 个答案:

答案 0 :(得分:2)

那是因为继承的接口声明了

IDbSet<CheckingAccount> checkingAccounts { get; set; }

并且在他们的实施中

 public IDbSet<CheckingAccount> checkAccounuts { get; set; }

checkAccounuts而不是checkingAccounts,它与接口定义的合同不匹配。因此编译时错误。