3

I am trying to track if it is possible, in Entity Framework, to check if database table has new records added. I am trying to get records from controller only if there is any new record added to database table. any suggestion?

Server:

        [HttpGet]

        public JsonResult DisplayChatMsgs()
        {
            var chatMsgs = dbObj.tblChats.ToList();
            return Json(chatMsgs, JsonRequestBehavior.AllowGet);
        }

Client:

        $.ajax({
            url: "Default/DisplayChatMsgs",
            type: "Get",
            success: function (data) {
                //var mdata = $.parseJSON(data.d);
                //The Div to be populated
                $('#msgBox').empty();

                content = "";
                //Looping thru each record
                $.each(data, function (i, record) {
                    //Properties available in Model
                    //We need to specify the properties in our model
                    content += "<tr><td><b>" + record.toName + "</b>:</td><td>" + record.chatMsg + "</td></tr>";

                });
                table = "<table>" + content + "</table>"
                $(table).appendTo('#msgBox');
                $("#msgBox").animate({ scrollTop: $('#msgBox')[0].scrollHeight }, 1000);
            }
        });

1 Answer 1

2

I am 99.99% sure that there is a built in way in EF to get notification when a row is added to DB.

A workaround you cold try is to use SignalR: each time your app writes to that table you publish a message to the client to get new data. The unelegant part of this approach is that you have to tap in to every place where you write to that table. However it would be great if you use the Repository Pattern, then you only have to make the change in one place.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, SignalR is the way to go, as it seems you're trying to build a chat application. Before the advent of pubsub, this type of thing was done on the web via Flash or Java precisely because trying to long-poll via JavaScript and differentiate between what's "old" vs. "new" asynchronously is a bloody nightmare if not totally impossible to get right.
And most common tutorials about SignalR are the ones that build chat apps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.