Refresh with RefreshMode.OverwriteCurrentValues

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
-marcelo-
Posts: 26
Joined: Wed 28 Jun 2017 12:35

Refresh with RefreshMode.OverwriteCurrentValues

Post by -marcelo- » Sun 14 Oct 2018 22:54

Premise: I have a List<Entity> obtained as a query result, wrapped in a BindingList<Entity>.
The user ask to update some of the instances (let it be the third one in the list). Then, to be kind, the app does a

Code: Select all

Entity currentState = toBeUpdated.Clone(); // generated Clone() method 
// the dataContext is the same used to retrieve the original List<Entity> from a query
dataContext.Refresh( RefreshMode.OverwriteCurrentValues, toBeUpdated); // check if someone updated the record 
if ( ! currentState.Compare(toBeUpdated) // extension method which compares property by property using reflection 
    return "record was modified";
 else
    return "updateIt!".
I think the previous code is OK regarding instance´s changes. What I don´t know is if the "third entity" in the binding list will be updated (even if I must force a rebind in the UI), or if I must update it in the BindingList programmatically.

Also (and very important), what is the result of Refresh() if the instance passed as argument was deleted?
Does it throws an exception?

TIA

Shalex
Site Admin
Posts: 8375
Joined: Thu 14 Aug 2008 12:44

Re: Refresh with RefreshMode.OverwriteCurrentValues

Post by Shalex » Thu 18 Oct 2018 12:58

-marcelo- wrote:
Sun 14 Oct 2018 22:54
What I don´t know is if the "third entity" in the binding list will be updated (even if I must force a rebind in the UI), or if I must update it in the BindingList programmatically.
You should force a rebind in the UI.
-marcelo- wrote:
Sun 14 Oct 2018 22:54
Also (and very important), what is the result of Refresh() if the instance passed as argument was deleted?
Does it throws an exception?
If toBeUpdated is null, you will get NullReferenceException.

-marcelo-
Posts: 26
Joined: Wed 28 Jun 2017 12:35

Re: Refresh with RefreshMode.OverwriteCurrentValues

Post by -marcelo- » Tue 23 Oct 2018 11:23

Thank you, Shalex, but I´m not sure I exposed clearly my second question (
what is the result of Refresh() if the instance passed as argument was deleted?
)
Let me reword it.
Suppose that from the initial query, some Entity, say, with ID = 256 is among the results.
Later, the user wants to uodate it. But in the middle, another user has deleted that instance from the database.
Nevertheless, for the first user, that instance is valid (is tracked and in the cache). So, the app executes

Code: Select all

dataContext.Refresh( RefreshMode.OverwriteCurrentValues, entitywithid256); 
Of course, Refresh() should fail, because the required instance no longer exists in the database.
How it signals that event?
Note: "entitywithid256" is not null for the first user app instance.
TIA

-marcelo-
Posts: 26
Joined: Wed 28 Jun 2017 12:35

Re: Refresh with RefreshMode.OverwriteCurrentValues

Post by -marcelo- » Tue 23 Oct 2018 14:31

Sorry, I supposed my second question was somewhat confused.
In fact, DataContext.Refresh(RefreshMode, object) fires a NullReferenceException when the object passed as argument doesn´t exists anymore in the dabase.
So, it´s neccessary to try/catch around Refresh...

-marcelo-
Posts: 26
Joined: Wed 28 Jun 2017 12:35

Re: Refresh with RefreshMode.OverwriteCurrentValues

Post by -marcelo- » Tue 23 Oct 2018 22:35

In fact, when the (local) current instance was deleted from the database, on Refresh() DataContext can throw any of two Exceptions: InvalidOperation or NullReference. The first is more frequent. My code looks like this

Code: Select all

        public override RefreshResult Refresh() 
        {
            Season local = (Season)ShallowCopy(false, dataContext);

            PackingSecurityDataContext context = dataContext as PackingSecurityDataContext;

            RefreshResult result = RefreshResult.NotChanged;

            try { 
                context.Refresh(RefreshMode.OverwriteCurrentValues, this);
                if (!context.Compare(this, local, false))
                    result = RefreshResult.Changed;

            }
            catch (InvalidOperationException) {
                result = RefreshResult.Deleted;
            }
            catch (NullReferenceException) {
                result = RefreshResult.Deleted;
            }

            return result;

        }        


Using last release of LinqConnect.

Shalex
Site Admin
Posts: 8375
Joined: Thu 14 Aug 2008 12:44

Re: Refresh with RefreshMode.OverwriteCurrentValues

Post by Shalex » Fri 26 Oct 2018 18:56

We are processing your request. We will contact you as soon as possible.

Shalex
Site Admin
Posts: 8375
Joined: Thu 14 Aug 2008 12:44

Re: Refresh with RefreshMode.OverwriteCurrentValues

Post by Shalex » Fri 09 Nov 2018 11:49

We have reproduced the issue and are investigating it. We will notify you about the result.

Post Reply