Migrations is enabled for context ''but th

2019-05-14 17:18发布

问题:

CreditoImobiliarioBB.Web

All interface are defined here. Has reference to the project CreditoImobiliarioBB.EntityFramework and EF6.
No configuration (we use FluentApi) or migrations is defined here.

CreditoImobiliarioBB.EntityFramework

Here is defined our generic repository and DbContext. All configurations and migrations are implemented here.

CreditoImobiliarioBB.Domain

Our design domain. Here are all our domain classes and interfaces (eg IEntity)

CreditoImobiliarioBB.Repository

Here is the repository implementation. For example EmployeesRepository

Error

When we run our application, the following error occurs:

Migrations is enabled for context 'CreditoImobiliarioContext' but the database does not exist or contains no mapped tables. Use Migrations to create the database and its tables, for example by running the 'Update-Database' command from the Package Manager Console.

Considerations

  1. The database has been created with all configurations aka EntityTypeConfiguration<> via the 'Update-Database'

  2. The error occurs in CreditoImobiliarioBB.EntityFramework project in a generic class Repository

Repository.cs

Error in line return _unitOfWork.Context.Set<T>().Where(expression);

public abstract class Repository<T> : IRepository<T>
        where T : class, IEntity
{
    private readonly EFUnitOfWork _unitOfWork;
    public Repository(IUnitOfWork unitOfWork)
    {
        _unitOfWork = (EFUnitOfWork)unitOfWork;
    }

    public void Delete(T obj)
    {
        _unitOfWork.Context.Set<T>().Remove(obj);
    }

    public void Store(T obj)
    {
        if (_unitOfWork.Context.Entry(obj).State == System.Data.Entity.EntityState.Modified)
            _unitOfWork.Context.Set<T>().Attach(obj);
        else
            _unitOfWork.Context.Set<T>().Add(obj);
    }

    public IQueryable<T> All()
    {
        return _unitOfWork.Context.Set<T>();
    }

    public object Get(Type entity, int id)
    {
        return _unitOfWork.Context.Set(entity).Find(id);
    }

    public T Get(Expression<Func<T, bool>> expression)
    {
        return _unitOfWork.Context.Set<T>().SingleOrDefault(expression);
    }

    public T Get(int id)
    {
        return _unitOfWork.Context.Set<T>().Find(id);
    }

    public IQueryable<T> Query(Expression<Func<T, bool>> expression)
    {
        return _unitOfWork.Context.Set<T>().Where(expression);
    }

    public IUnitOfWork UnitOfWork
    {
        get { return _unitOfWork; }
    }
}