AspnetBoilerplate 模块化:实体类型 XXX 不是当前上下文模型的一部分
AspnetBoilerplate modular: The entity type XXX is not part of the model for the current context
抱歉我的英语不好,谁能帮我解决这个错误,如下所示:
我已经按照这个 tutorial 在 AspnetZero 框架(名为 FastOne)上创建了一个新的模块应用程序(名为 Payment)。
在第二个 DbContext 中,我将默认连接更改为我的第二个数据库并添加新的实体名称 BankCode。添加迁移和更新数据库后,一切正常。使用 1 table name BankCode
创建了新数据库
我去.Application模块项目中创建了一个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();
}
}
}
这是我的 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) { }
}
api创建成功,调用时返回如下错误:
The entity type BankCode is not part of the model for the current
context.
我尝试调试并意识到存储库是 FastOneDbContext 而不是 PaymentDbContext :[调试时存储库错误][2]
我对 AspNetBoilerplate 和模块零完全陌生。所以任何人都可以帮助我。感谢
更新:这是我的 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
感谢您的帮助亚伦,一切正常,这是最新的错误,我已经搜索了一些解决方案,但它们没有用。请帮忙
No component for supporting the service
FastOne.EntityFramework.PaymentDbContext was found
更新 4
我试图将此方法添加到 PaymentDataModule:
public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
}
成功了!我可以看到 _repository 获得了正确的上下文。但是当我 运行 时,它引发了这个错误:
System.Data.SqlClient.SqlException: Invalid object name
'dbo.BankCode'.
我发现连接错了。这是第一个上下文的connstring
更新5:我发现我在PaymentDbContext
中有3个构造函数。所以我删除了这些行:
public PaymentDbContext() : base("LocalPaymentConn") { }
//public PaymentDbContext(string nameOrConnectionString) : base(nameOrConnectionString) { }
//public PaymentDbContext(DbConnection connection) : base(connection) { }
-->它收到了正确的连接字符串。好棒!!感谢 Aaron 和 Tien 的帮助。谢谢
您应该按照您在 AutoRepositoryTypes
中指定的方式注入 IPaymentRepositoryBase
。
public class BankCodeAppService : FastOneAppServiceBase, IBankCodeAppService
{
private readonly IPaymentRepositoryBase<BankCode> _repository;
public BankCodeAppService(IPaymentRepositoryBase<BankCode> repository)
{
_repository = repository;
}
}
更新 1
ComponentActivator: could not instantiate FastOne.EntityFramework.Repositories.PaymentRepositoryBase`1[[FastOne.BankCode.BankCode, FastOne.Payment.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
从 PaymentRepositoryBase<TEntity>
的 class 声明中删除 abstract
关键字。
更新 2
Target does not implement interface FastOne.Domain.Repositories.IPaymentRepositoryBase`1[[FastOne.BankCode.BankCode, FastOne.Payment.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]\r\nParameter name: target
使PaymentRepositoryBase
实现IPaymentRepositoryBase
。
public class PaymentRepositoryBase<TEntity> : PaymentRepositoryBase<TEntity, int>, IPaymentRepositoryBase<TEntity>
where TEntity : class, IEntity<int>
{
public PaymentRepositoryBase(IDbContextProvider<PaymentDbContext> dbContextProvider) : base(dbContextProvider) { }
}
您正在应用中使用多个 dbcontext。所以第二个上下文必须继承自 AbpDbContext
才能正确注入 ABP。请参阅 https://github.com/aspnetboilerplate/aspnetboilerplate-samples 上的 MultipleDbContextDemo
示例。
希望对您有所帮助。
抱歉我的英语不好,谁能帮我解决这个错误,如下所示:
我已经按照这个 tutorial 在 AspnetZero 框架(名为 FastOne)上创建了一个新的模块应用程序(名为 Payment)。
在第二个 DbContext 中,我将默认连接更改为我的第二个数据库并添加新的实体名称 BankCode。添加迁移和更新数据库后,一切正常。使用 1 table name BankCode
创建了新数据库我去.Application模块项目中创建了一个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();
}
}
}
这是我的 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) { }
}
api创建成功,调用时返回如下错误:
The entity type BankCode is not part of the model for the current context.
我尝试调试并意识到存储库是 FastOneDbContext 而不是 PaymentDbContext :[调试时存储库错误][2] 我对 AspNetBoilerplate 和模块零完全陌生。所以任何人都可以帮助我。感谢
更新:这是我的 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 感谢您的帮助亚伦,一切正常,这是最新的错误,我已经搜索了一些解决方案,但它们没有用。请帮忙
No component for supporting the service FastOne.EntityFramework.PaymentDbContext was found
更新 4 我试图将此方法添加到 PaymentDataModule:
public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
}
成功了!我可以看到 _repository 获得了正确的上下文。但是当我 运行 时,它引发了这个错误:
System.Data.SqlClient.SqlException: Invalid object name 'dbo.BankCode'.
我发现连接错了。这是第一个上下文的connstring
更新5:我发现我在PaymentDbContext
中有3个构造函数。所以我删除了这些行:
public PaymentDbContext() : base("LocalPaymentConn") { }
//public PaymentDbContext(string nameOrConnectionString) : base(nameOrConnectionString) { }
//public PaymentDbContext(DbConnection connection) : base(connection) { }
-->它收到了正确的连接字符串。好棒!!感谢 Aaron 和 Tien 的帮助。谢谢
您应该按照您在 AutoRepositoryTypes
中指定的方式注入 IPaymentRepositoryBase
。
public class BankCodeAppService : FastOneAppServiceBase, IBankCodeAppService
{
private readonly IPaymentRepositoryBase<BankCode> _repository;
public BankCodeAppService(IPaymentRepositoryBase<BankCode> repository)
{
_repository = repository;
}
}
更新 1
ComponentActivator: could not instantiate FastOne.EntityFramework.Repositories.PaymentRepositoryBase`1[[FastOne.BankCode.BankCode, FastOne.Payment.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
从 PaymentRepositoryBase<TEntity>
的 class 声明中删除 abstract
关键字。
更新 2
Target does not implement interface FastOne.Domain.Repositories.IPaymentRepositoryBase`1[[FastOne.BankCode.BankCode, FastOne.Payment.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]\r\nParameter name: target
使PaymentRepositoryBase
实现IPaymentRepositoryBase
。
public class PaymentRepositoryBase<TEntity> : PaymentRepositoryBase<TEntity, int>, IPaymentRepositoryBase<TEntity>
where TEntity : class, IEntity<int>
{
public PaymentRepositoryBase(IDbContextProvider<PaymentDbContext> dbContextProvider) : base(dbContextProvider) { }
}
您正在应用中使用多个 dbcontext。所以第二个上下文必须继承自 AbpDbContext
才能正确注入 ABP。请参阅 https://github.com/aspnetboilerplate/aspnetboilerplate-samples 上的 MultipleDbContextDemo
示例。
希望对您有所帮助。