Migrations is enabled for context ''but the database does not exist or contains no mapped tables

倖福魔咒の 提交于 2020-01-23 09:50:12

问题


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; }
    }
}

来源:https://stackoverflow.com/questions/17476239/migrations-is-enabled-for-context-but-the-database-does-not-exist-or-contains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!