How to include object properties in EntityFrameworkCore InMemory

.net-core .net-core-2.1 ef-core-2.1 entity-framework entity-framework-core

Question

I want to test the DbContext with EntityFramework.InMemory, but when I try to include all objects properties, they are null, and I really don't know why.

I have a UnitOfWorkLocator that is responsible to create and return the same UnitOfWork.

public static class UnitOfWorkLocator
{
     public static Func<UnitOfWork> UnitOfWorkFactory;

     public static UnitOfWork GetUnitOfWork()
     {
         return UnitOfWorkFactory();
     }
}

So in every test class I do something like this:

contextOptions = new DbContextOptionsBuilder<SchoolLibraryContext>()
                .UseInMemoryDatabase(Guid.NewGuid().ToString())
                .Options;
UnitOfWorkLocator.UnitOfWorkFactory = () => new UnitOfWork(contextOptions);

LendedBook:

public class LendedBook: EntityBase
{
    public virtual Book Book
    {
        get;
        set;
    }
    public virtual Student Student
    {
        get;
        set;
    }
    public virtual DateInterval DateInterval
    {
        get;
        set;
    }
    public bool Returned
    {
        get;
        set;
    }
    public bool Canceled
    {
        get;
        set;
    }
}

The method from repository:

public LendedBook GetLendedBookById(Guid lendedBookId)
{
    return schoolLibraryContext.LendedBooks.Include(book => book.Book)
       .Include(student => student.Student)
       .Include(dateInterval => dateInterval.DateInterval)
       .FirstOrDefault(lendedBook => lendedBook.Id == lendedBookId) ??
       throw new EntityNotFoundException(typeof(LendedBook).Name);
}

The EntityBase class is responsible to save entity in the database with this method:

public void Save()
{
      using (var unitOfWork = UnitOfWorkLocator.GetUnitOfWork())
      {
            unitOfWork.AddOrUpdate(this);
            unitOfWork.Commit();
      }
}

And EntityBase also have the Id property:

public Guid Id { get; set; }

The initialization and the Save method executed on the LendedBook:

var student = new Student("Sebastian", "Odegrad")
{
     Email = "email@domain.com"
};
var dateInterval = new DateInterval
{
     StartDate = new DateTime(2018, 08, 01),
     EndDate = new DateTime(2018, 08, 30)
};
var lendedBook = new LendedBook
{
     Student = student,
     DateInterval = dateInterval
};
libranian = new Libranian("Wilskinson", "Martin");

student.Save();
dateInterval.Save();
lendedBook.Save();

So, how can I include all the object properties correctly with EFCore.InMemory?

1
0
8/22/2018 8:47:28 AM

Accepted Answer

I found one solution:

I changed the parameter from the constructor of the UnitOfWork to accept a DbContext instead of DbContextOptions. And I also removed the Save() method from EntityBase and I created AddLendedBook in the LendedBookRepository. I think this approach is much more clean and testable.

0
8/22/2018 10:45:37 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow