对不起,我的英语不好,谁能帮我解决这个错误如下:
我遵循了这一点
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) { }
--&燃气轮机;它收到了正确的连接字符串。太棒了!!谢谢亚伦和田的帮助。非常感谢。