Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Insert data into Multiple db context in single service #9429


User avatar
0
[email protected] created

Hi Team, I've gone thorugh following link and implemented similiar way, But I'm unable to insert data into different databases in single service call. https://github.com/aspnetboilerplate/aspnetboilerplate-samples/tree/master/MultipleDbContextEfCoreDemo Following below is the error which i'm getting: Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot insert explicit value for identity column in table 'XXX' when IDENTITY_INSERT is set to OFF. at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__164_0(Task1 result) at System.Threading.Tasks.ContinuationResultTaskFromResultTask2.InnerInvoke() at System.Threading.Tasks.Task.<>c.<.cctor>b__274_0(Object obj) 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.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread) --- End of stack trace from previous location where exception was thrown --- at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken) ClientConnectionId:30c970df-00b5-4599-b270-950cd92bfdbc Error Number:544,State:1,Class:16 --- End of inner exception stack trace --- at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, Cancellat...

Requirement is as follows below: First I would like to insert data and retreive ID and assign value to the property of other table in second database and insert the data into second database

Can you please help me out to fix the issue.

Thanks in Advance, Prasanna.


5 Answer(s)
  • User Avatar
    0
    [email protected] created

    FIxed issue and working as expected, My Bad missed few things while implementing.

  • User Avatar
    0
    [email protected] created

    Hi,

    The requirement is as follows: We'll be adding data in one project, on the fly data should be inserted into other database with the reference of ids from first database tables. we are using aspnet zero projects.

    For example: If i create a tenant in first database, corresponding all tables which are effected with tenant creation data should be dumped into second database.

    Implemented using below sample code https://github.com/aspnetboilerplate/aspnetboilerplate-samples/tree/master/MultipleDbContextEfCoreDemo. But I'm able to insert only Tenant, user, role tables information, & others tables information are not getting inserting (AbpUserAccounts, AbpPermissions, Notifications) and I'm getting error if i include code related to UserRole in tenant creation of second db context.

    System.InvalidOperationException: Cannot use multiple DbContext instances within a single query execution. Ensure the query uses a single context instance. at Microsoft.EntityFrameworkCore.Query.Internal.ParameterExtractingExpressionVisitor.VisitConstant(ConstantExpression constantExpression) at System.Linq.Expressions.ConstantExpression.Accept(ExpressionVisitor visitor) at Microsoft.EntityFrameworkCore.Query.Internal.ParameterExtractingExpressionVisitor.Visit(Expression expression) at System.Dynamic.Utils.ExpressionVisitorUtils.VisitArguments(ExpressionVisitor visitor, IArgumentProvider nodes) at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node) at Microsoft.EntityFrameworkCore.Query.Internal.ParameterExtractingExpressionVisitor.Visit(Expression expression) at System.Dynamic.Utils.ExpressionVisitorUtils.VisitArguments(ExpressionVisitor visitor, IArgumentProvider nodes) at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node) at Microsoft.EntityFrameworkCore.Query.Internal.ParameterExtractingExpressionVisitor.Visit(Expression expression) at System.Dynamic.Utils.ExpressionVisitorUtils.VisitArguments(ExpressionVisitor visitor, IArgumentProvider nodes) at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node) at Microsoft.EntityFrameworkCore.Query.Internal.ParameterExtractingExpressionVisitor.Visit(Expression expression) at Microsoft.EntityFrameworkCore.Query.Internal.ParameterExtractingExpressionVisitor.ExtractParameters(Expression expression) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExtractParameters(Expression query, IParameterValues parameterValues, IDiagnosticsLogger`1 logger, Boolean parameterize, Boolean generateContextAccessors) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken) at Microsoft.En...

    Can you please help me out how to acheive my requirement ?

    Thanks.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Could you share your code which makes the insertion ?

  • User Avatar
    0
    [email protected] created

    Hi @ismcagdas,

    I can't share the code due to company policy. but would like to let you know that i've made replica of User, User Manager, User Store, Role, Role Manager, Role Store, Tenant Manager using second db context.

    Thanks, Prasanna.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @[email protected]

    Thanks. You shouldn't insert the same ID values for the second DbContext. Probably that's why you are getting this error. If you can share a simple reproduction project with [email protected], we can try to help you.