how to replace replace IObjectContextAdapter with IInfrastructure<IServiceProvider> using entity framework core 6

Firke, Yogesh (Cognizant) 0 Reputation points
2023-03-29T11:47:17.0866667+00:00

Hi Team,

since IObjectContextAdapter is not supported by entity framework core 6 please suggest the replacement code which supports the below code -

 using (var db = new BloggingContext())
            {
                var blogs = ((IObjectContextAdapter)db)
                    .ObjectContext
                    .Translate<Blog>("pass reader object here", "pass ur dbset/table name here", "MergeOption.AppendOnly");
            }
Developer technologies .NET Entity Framework Core
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2023-03-30T04:24:51.5566667+00:00

    @Firke, Yogesh (Cognizant), based on my research, it seems that there is no fully equivalent method of the Translate method.

    However, you could look at the following workaround to check if it works for you.

                EfContext context = new EfContext();
                var sql = "SELECT * FROM Members WHERE Name = @p0";
                var parameters = new[] { new SqlParameter("@p0", "Test") };
                var result = context.Members.FromSqlRaw(sql, parameters).ToList();
    
    

    You could use sqlcommand parameters directly, MergeOption.AppendOnly is the default behavior and you don't need to set it.

    If you think the workaround does help you, you also could submit the issue in GitHub.

    Hope my wordaround could help you.

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.