-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;
}
}
}
你真棒,非常感謝你:) – Dodi