N
N
Ness2019-09-07 20:47:43
C++ / C#
Ness, 2019-09-07 20:47:43

DB dependencies in EF Core in conjunction with using?

Actually, the question is, there is a telegram handler, I catch callbacks there and work out using switch cases in using (catching likes or dislikes), how can I throw dependencies into these uses? I searched on Google, it’s just how to add context to dependencies, but I didn’t even find how to use these dependencies in using, here is a piece of my code:

internal partial class TelegramHandler
    {
        const string channelId = "@linkd_1";

        private async Task HandleCallbackQuery(CallbackQuery callbackQuery, CancellationToken cancellationToken)
        {
            switch (callbackQuery.Data)
            {
                case "post_id like":
                    {
                        using (var db = new MyContext())
                        {
                            var postData = db.Posts
                                        .FirstOrDefault(x => x.MessageId == callbackQuery.Message.MessageId);
                            //var postData = conn.ExecuteReader($"SELECT * FROM PostData AS pd WHERE pd.MessageId = '{ callbackQuery.Message.MessageId }'").Parse<PostData>().FirstOrDefault();
                            if (postData == null)
                            {
                                return;
                            }

                            var reader = db.Votes.FirstOrDefault(x =>
                                ((x.VoterId == callbackQuery.From.Id) && (x.PostDataId == postData.Id)));

Now I just use an empty constructor, here is my db context: https://gist.github.com/Taifunov/7db7c19f86e20eca4...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question