hi hikalkan ,
First,my project based on ABP created two dbcontext. I have modified the method 'UsingDbContext' of AbpTestBase as generic method to initialize two dbcontext data at AppTestBase intialize. The modified method like below:
protected void UsingDbContext<TDbContext>(Action<TDbContext> action)
where TDbContext:AbpDbContext
{
using (var context = LocalIocManager.Resolve<TDbContext>())
{
context.DisableAllFilters();
action(context);
context.SaveChanges();
}
}
Below is AbpTestBase construction method :
protected AppTestBase()
{
//Seed initial data
UsingDbContext<HWWLDbContext>(context =>
{
new InitialDbBuilder(context).Create();
new TestDataBuilder(context).Create();
});
LoginAsDefaultTenantAdmin();
UsingDbContext<SecondHWWLDbContext>(secondSontext =>
{
new LMTestDataBuilder(secondSontext,AbpSession.TenantId.Value).Create();
});
}
Below is LMTestDataBuilder.cs :
public class LMTestDataBuilder
{
private readonly int _tenantId;
private readonly SecondHWWLDbContext _secondContext;
public LMTestDataBuilder(SecondHWWLDbContext secondContext,int tenantId)
{
_tenantId = tenantId;
_secondContext = secondContext;
}
public void Create()
{
_secondContext.DisableAllFilters();
CreateCargo();
_secondContext.SaveChanges();
}
public void CreateCargo()
{
var cargo = new Cargo
{
Id = GuidHelper.GetCombinedGuid(),
TenantId = _tenantId,
OperationTime = DateTime.Now,
InternalNumber = "TR1001",
CargoName = "Car",
NumberOfPackage = 100,
DeliveryAmount = 100,
ChargeAmount = 100,
RowVersion=Encoding.UTF8.GetBytes(Guid.NewGuid().ToString())
};
_secondContext.Cargoes.Add(cargo);
}
}
it threw the exception : '{"Table 'AndCargoes' was not found. The database was probably not initialized.\r\n\r\nIf using CodeFirst try to add the following line:\r\ncontext.Database.CreateIfNotExists()"}' when initializing the second dbcontext data.
6 Answer(s)
-
0
Anybody can help me?
-
0
Hi,
Please share your stack trace and second dbcontext (I suspect that the 2nd dbcontext has no costructor takes DbConnection). Also, do you have single shared db for 2 dbcontextes?
-
0
hi hikalkan, I have single shared db for 2 dbcontextes and unit test can run correctly. My 2nd dbcontext has the costructor takes DbConnection like below:
public class SecondHWWLDbContext : AbpDbContext {
public virtual IDbSet<Notification> Notifications { get; set; } public SecondHWWLDbContext() : base("Second") { } public SecondHWWLDbContext(string secondNameOrConnectionString) : base(secondNameOrConnectionString) { } public SecondHWWLDbContext(DbConnection dbConnection) : base(dbConnection, true) { } }
Stack trace is like below: at Abp.EntityFramework.AbpDbContext.SaveChanges() location e:\Abp\trunk\Abp+Zero+HWWL\Abp.EntityFramework\EntityFramework\AbpDbContext.cs:line 190 at AndHuang.HWWL.TestDatas.LMTestDataBuilder.Create() location e:\Abp\trunk\Abp+Zero+HWWL\AndHuang.HWWL.Tests\TestDatas\LMTestDataBuilder.cs:line 42 at AndHuang.HWWL.Tests.AppTestBase.<.ctor>b__1(SecondHWWLDbContext secondSontext) location e:\Abp\trunk\Abp+Zero+HWWL\AndHuang.HWWL.Tests\AppTestBase.cs:line 45 at AndHuang.HWWL.Tests.AppTestBase.UsingDbContext[TDbContext](Action`1 action) location e:\Abp\trunk\Abp+Zero+HWWL\AndHuang.HWWL.Tests\AppTestBase.cs:line 77 at AndHuang.HWWL.Tests.AppTestBase..ctor() location e:\Abp\trunk\Abp+Zero+HWWL\AndHuang.HWWL.Tests\AppTestBase.cs:line 43 at AndHuang.HWWL.Bills.Bill_Tests..ctor() location e:\Abp\trunk\Abp+Zero+HWWL
-
0
Hi,
Maybe effort do not support multiple dbcontext. I have never tried it. Probably you opened this issue: <a class="postlink" href="https://github.com/tamasflamich/effort/issues/44">https://github.com/tamasflamich/effort/issues/44</a> We wait for an answer.
-
0
Ok,thank you!
-
0
Hi @JackyWang ,
Do you find solution for it ? I'm still on it.
Thank you.