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.
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 .