简体   繁体   中英

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.

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. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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