代码之家  ›  专栏  ›  技术社区  ›  Hoang Tran

AspnetBoilerplate模块化:实体类型XXX不是当前上下文的模型的一部分

  •  0
  • Hoang Tran  · 技术社区  · 8 年前

    对不起,我的英语不好,谁能帮我解决这个错误如下:

    我遵循了这一点 tutorial

    在第二个DbContext中,我将默认conn更改为第二个数据库,并添加新的实体名称BankCode。添加迁移和更新数据库后,一切正常。使用1个表名BankCode创建了新数据库

    我在中创建了AppService。应用模块项目如下:

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

    [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) { }
        }
    

    api创建成功,但当我调用它时,它返回了以下错误:

    实体类型银行代码不是当前

    我试着调试并意识到存储库是FastOneDbContext而不是PaymentDbContext:[调试时存储库错误][2]

    更新:这是我的 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)
        }
    }
    

    更新3 谢谢你的帮助,亚伦,一切正常,这是最新的错误,我已经搜索了一些解决方案,但他们没有工作。请帮忙

    没有支持服务的组件 法斯通。实体框架。找到PaymentDbContext

    更新4

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

    成功了!,我可以看到存储库获得了正确的上下文。但当我运行时,它引发了以下错误:

    'dbo。银行代码'。

    我意识到连接是错误的。它是第一个上下文的连接字符串

    更新5 PaymentDbContext

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

    --&燃气轮机;它收到了正确的连接字符串。太棒了!!谢谢亚伦和田的帮助。非常感谢。

    2 回复  |  直到 8 年前
        1
  •  1
  •   aaron    8 年前

    你应该注射 IPaymentRepositoryBase AutoRepositoryTypes

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

    更新1

    ComponentActivator:无法实例化FastOne。实体框架。存储库。PaymentRepositoryBase'1[[FastOne.BankCode.BankCode,FastOne.Payment.Core,版本=1.0.0.0,区域性=中性,公钥令牌=null]]

    abstract 类声明中的关键字 PaymentRepositoryBase<TEntity> .


    更新2

    制作 PaymentRepositoryBase IPaymentRepositoryBase .

    public class PaymentRepositoryBase<TEntity> : PaymentRepositoryBase<TEntity, int>, IPaymentRepositoryBase<TEntity>
        where TEntity : class, IEntity<int>
    {
        public PaymentRepositoryBase(IDbContextProvider<PaymentDbContext> dbContextProvider) : base(dbContextProvider) { }
    }
    
        2
  •  0
  •   tiennguyen    8 年前

    你正在应用程序中使用multilple dbcontext。所以第二个上下文必须继承自 AbpDbContext MultipleDbContextDemo 上的示例 https://github.com/aspnetboilerplate/aspnetboilerplate-samples .

    希望这对你有帮助。

    推荐文章