Ok,thank you!
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
Anybody can help me?
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.
Thanks a lot , it gave me much help.
Stack Trace is like below: [External code]
Abp.EntityFramework.dll!Abp.EntityFramework.AbpDbContext.Initialize() line 137 C# [External code] Abp.dll!Abp.Dependency.IocManager.Resolve<AndHuang.HWWL.EntityFramework.HWWLDbContext>() line 159 C# AndHuang.HWWL.dll!AndHuang.HWWL.Tests.AppTestBase.UsingDbContext(System.Action<AndHuang.HWWL.EntityFramework.HWWLDbContext> action) line 67 C# AndHuang.HWWL.dll!AndHuang.HWWL.Tests.AppTestBase.AppTestBase() line 34 C# AndHuang.HWWL.dll!AndHuang.HWWL.Test.Sms.SmsManager_Tests.SmsManager_Tests() line C# [External code]
I viewed the link you provided ,I guess it may be a bug of EF CodeFirst. .
Hi hikanlkan,
I met the error 'The sequence does not contain any matching elements' when I did the unit test of abp. I took me a long time to find the error code. My entity has the column attribute for one property like that: [Column(TypeName = "varchar")] public virtual string Picture { get; set; }
When I used the method of UsingDbContext of AppTestBase , the error was throw. I don't konw why the column attribute causes the error?
Hi hikanlkan, When I used the Unit Test of ABP , the error like 'The sequence does not contain any matching elements' happend in the method 'Initialize' of AbpDbContext. It took me a long time to find the error code . I found the error was caused by one property of an entity was added the attribute 'Column',like that :
[Column(TypeName = "varchar")] public virtual string Picture { get; set; }
But, I learnt some aout Code First , which has the attribute of 'Column', so why the column attribute caused the error? Thank you .