2016-11-26 52 views
-1

我意識到這個問題之前已經被問過。不過,我嘗試了以前的解決方案,並沒有一個爲我工作。我試圖隔離真正的數據庫,以便通過使用假數據庫上下文運行模擬依賴項測試。我創建了這個原因的ApplicationBbContext,但有困難要弄清楚爲什麼會出現與應用的DbContext錯誤不實現接口成員錯誤應用程序DbContext沒有實現接口成員錯誤AS.NET MVC

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


    } 
} 

回答

2

這是因爲繼承的接口聲明

IDbSet<CheckingAccount> checkingAccounts { get; set; } 

,並在實現他們

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

checkAccounuts代替checkingAccounts它通過接口定義的合同不匹配。因此編譯時錯誤。

+0

你真棒,非常感謝你:) – Dodi

相關問題