I need to import some data (tenants, users, organzation etc) from legacy system. and i want keep the id column with the same value. so i need to set the id value explictly.
await dbContext.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.AbpTenants ON");
using (var uow = _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
{
var legacyTenants = await _legacySysTenantRepository.GetAll().ToListAsync();
foreach (var legacy in legacyTenants)
{
var tenantId = await _tenantManager.CreateWithAdminUserAsync(
tenancyName: legacy.BusinessCode,
name: legacy.TenantName,
adminEmailAddress: $"admin@{legacy.TenantDomain}",
adminPassword: $"xxxxxxxxxx",
connectionString: null,
isActive: legacy.Enabled == 0,
editionId: 1,
shouldChangePasswordOnNextLogin: false,
sendActivationEmail: false,
subscriptionEndDate: null,
isInTrialPeriod: false,
emailActivationLink: null,
specifiedId: legacy.Id // i changed this methods to assing Id to AbpTenants`s Id
);
}
await uow.CompleteAsync();`
await dbContext.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.AbpTenants OFF");
but it throw exception
'
{System.Data.SqlClient.SqlException (0x80131904): Cannot insert explicit value for identity column in table 'AbpTenants' when IDENTITY_INSERT is set to OFF.
at System.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__122_0(Task1 result) at System.Threading.Tasks.ContinuationResultTaskFromResultTask
2.InnerInvoke()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteAsync(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
ClientConnectionId:30b0f3f6-84ff-4a73-afd1-0579643388c6
Error Number:544,State:1,Class:16}
'
@ismcagdas once i want to inject IDbContextProvider to AppService`s ctor, get an exception 'could not proxy xxxxAppService'. where and how to get DbContext?
@ismcagdas where is the best place to call FromSql in AspNet Zero?
@maliming thanks.
I add the logger factory from abp`s ILoggerFactory by DI.
i try a normal way to enable ef core`s logging by setting in appsetting.json
"Logging": { "LogLevel": { "Microsoft.EntityFrameworkCore": "Debug" } }
but i cann`t see any sql logging in log file.
@krileo how to enable / disable it ? i cann`t enable it
how to enable it ? i set appsetting.json , Logging:LogLevel. but logging any about ef.
@ismcagdas any feedback?