Search code examples
aspnetboilerplate

AspnetBoilerplate modular: The entity type XXX is not part of the model for the current context


Sorry for my bad English, Could anyone help me with this error as below:

I have followed this tutorial to create a new module application (named Payment) on AspnetZero framework (named FastOne).

In the second DbContext, I change the default conn to my second database and add new entity name BankCode. After add-migration and update-database, everything works well. New database was created with 1 table name BankCode

I went to create a AppService in .Application module project as below:

public class BankCodeAppService : FastOneAppServiceBase, IBankCodeAppService
    {
        //from PaymentDbContext
        private readonly IRepository<BankCode> _repository;

        public BankCodeAppService(IRepository<BankCode> repository)
        {
            _repository = repository;
        }

       public ListResultDto<BankCodeDto> GetAllBankCodeDto()
        {
            try
            {
                var result = _repository.GetAll().ToList();
                return new ListResultDto<BankCodeDto>(result.MapTo<List<BankCodeDto>>());
            }
            catch (Exception ex)
            {
                throw new NotImplementedException();
            }
        }
    }

This is my PaymentDbContext

[DefaultDbContext]
    [AutoRepositoryTypes(
        typeof(IPaymentRepositoryBase<>),
        typeof(IPaymentRepositoryBase<,>),
        typeof(PaymentRepositoryBase<>),
        typeof(PaymentRepositoryBase<,>)
    )]
    public class PaymentDbContext : FastOneDbContext
    {
        /* Define an IDbSet for each entity of the application */

        public virtual IDbSet<BankCode.BankCode> BankCodes { get; set; }

        //TODO: Define an IDbSet for your Entities...
        public PaymentDbContext() : base("SecondConn") { }
        public PaymentDbContext(string nameOrConnectionString) : base(nameOrConnectionString) { }
        public PaymentDbContext(DbConnection connection) : base(connection) { }
    }

The api was created successfully, but when I called to it, it returned the error as below:

The entity type BankCode is not part of the model for the current context.

I tried to debug and realize that the repository is FastOneDbContext instead of PaymentDbContext : [Wrong repository when debugging][2] I'm totally new with AspNetBoilerplate and module Zero. So could anyone please to help me. Thank

Update: This is my PaymentRepositoryBase

namespace FastOne.EntityFramework.Repositories
{
    public class PaymentRepositoryBase<TEntity, TPrimaryKey> : FastOneRepositoryBase<TEntity, TPrimaryKey>
        where TEntity : class, IEntity<TPrimaryKey>
    {
        public PaymentRepositoryBase(IDbContextProvider<PaymentDbContext> dbContextProvider) : base(dbContextProvider) { }
        //do not add any method here, add to the class above (since this inherits it)
    }
    public abstract class PaymentRepositoryBase<TEntity> : PaymentRepositoryBase<TEntity, int>
        where TEntity : class, IEntity<int>
    {
        public PaymentRepositoryBase(IDbContextProvider<PaymentDbContext> dbContextProvider) : base(dbContextProvider) { }
        //do not add any method here, add to the class above (since this inherits it)
    }
}

Update 3 Thank for your help Aaron, everything goes correctly, this is the newest error, I have searched some solutions but they didn't work. Please help

No component for supporting the service FastOne.EntityFramework.PaymentDbContext was found

Update 4 I tried to add this method to PaymentDataModule:

public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
        }

It worked!, I can see _repository get correct context. but when i run, it raised this error:

System.Data.SqlClient.SqlException: Invalid object name 'dbo.BankCode'.

I realized that the connection was wrong. It was the connstring of the first context

Update 5: I found that I have 3 constructors in PaymentDbContext. So i removed these lines:

public PaymentDbContext() : base("LocalPaymentConn") { }
        //public PaymentDbContext(string nameOrConnectionString) : base(nameOrConnectionString) { }
        //public PaymentDbContext(DbConnection connection) : base(connection) { }

-->It received the correct connection string. So awesome!! Thanks Aaron and Tien for all your help. Thank you


Solution

  • You should inject IPaymentRepositoryBase as you have specified in AutoRepositoryTypes.

    public class BankCodeAppService : FastOneAppServiceBase, IBankCodeAppService
    {
        private readonly IPaymentRepositoryBase<BankCode> _repository;
    
        public BankCodeAppService(IPaymentRepositoryBase<BankCode> repository)
        {
            _repository = repository;
        }
    }
    

    Update 1

    ComponentActivator: could not instantiate FastOne.EntityFramework.Repositories.PaymentRepositoryBase`1‌​[[FastOne.BankCode.B‌​ankCode, FastOne.Payment.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

    Remove abstract keyword from the class declaration for PaymentRepositoryBase<TEntity>.


    Update 2

    Target does not implement interface FastOne.Domain.Repositories.IPaymentRepositoryBase`1[[FastOn‌​e.BankCode.BankCode, FastOne.Payment.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]\r\nParameter name: target

    Make PaymentRepositoryBase implement IPaymentRepositoryBase.

    public class PaymentRepositoryBase<TEntity> : PaymentRepositoryBase<TEntity, int>, IPaymentRepositoryBase<TEntity>
        where TEntity : class, IEntity<int>
    {
        public PaymentRepositoryBase(IDbContextProvider<PaymentDbContext> dbContextProvider) : base(dbContextProvider) { }
    }