简体   繁体   English

使用实体框架和DbContext测试问题

[英]Testing problems with Entity framework and DbContext

I'm working on a project where I wish to test some classes that are using entity framework. 我正在一个项目中,我希望测试一些使用实体框架的类。 In that process I've created a test DbContext. 在此过程中,我创建了一个测试DbContext。

class FakeContext : DbContext, IDbContext
{
    public IDbSet<UserAcc> UserAcc { set; get; }
    public IDbSet<Movies> Movies { set; get; }

    public new IDbSet<T> Set<T>() where T : class
    {
        return base.Set<T>();
    }
}

[TestClass]
public class EFStorageTest
{
    private IStorageConnection _ef;

    [TestMethod]
    public void AddToContextTest()
    {
        _ef = new EFConnectionFactory().GetConnection<FakeContext>();
        _ef.Add(new UserAcc());
        _ef.SaveChanges();
        Assert.AreEqual(1, _ef.Get<UserAcc>().Count());
    }
 }

When i tried to add an entity to my context, i assumed that it would stay in memory, but it's being automatically saved somewhere by Visual Studio. 当我尝试将实体添加到上下文中时,我假设它会保留在内存中,但是Visual Studio会自动将其保存在某个位置。 Where is the automatically created database located? 自动创建的数据库位于哪里?

By default EF creates database on SQL server express with name equal to full data context class name. 默认情况下,EF在SQL Server Express上创建名称等于完整数据上下文类名称的数据库。 Ie in your case it will be 即在您的情况下

 server=.\SQLEXPRESS;database=Your.Namespace.FakeContext

BTW I do not recommend you to use real database in tests, if you are not doing acceptance testing - that is slow, brittle and time-consuming 顺便说一句,我不建议您在测试中使用真实的数据库,如果您不进行验收测试,那是缓慢,脆弱且耗时的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM