|
Use different context instances to maintain different sets of data that you can reload separately?
There's nothing that says you have to use a single context instance for everything. That way you can just destroy the context instance and create a new one, telling it to load the data you want all over again.
|
|
|
|
|
But the entities have relation between each other , how can I keep different contexts !!!!!?????
one entity is a child for other and is parent for others. How can I use these in different contexts ????
|
|
|
|
|
It wasn't clear in your original post if these records you're trying to reload were related to the other entities or not.
There is no fast way to do this. You could have the context execute a SQL query to reload just the records you want, but the context is still going to go through each and every result record to update the cached objected.
I think your design needs to be reconsidered if you have a user making changes that forces other users to reload a large amount of data. Minimize the data each user is caching as much as possible.
|
|
|
|
|
This is what i'm asking : a query to reload all the records from an entity.
If some of objects inside myobj are updated from other users , I need a query to reload all the objects in myobj from database. But I can't find a such query to do this so I have to reload one by one as on my 2 methods described in my first question.
It's strange why there's no a method on entity framework to force reloading all object from an entity from database and not read on local cache. !!!!!
|
|
|
|
|
Then you're going to have to tell the clients which records updated from the client that wrote to the database.
Now, every client is going to have to retrieve the object again using that records ID value. Retrieving a record by ID is easy. Reloading EVERY record you have cached is going to take forever, no matter what you do.
There is no solution around this.
Even without an ORM like EF, there's no real good way to do this! It's your application design that has the problem.
|
|
|
|
|
Sorry , I think this is a problem of Entity Framework Design.
Because , if other users add or delete records , when I run my query these changes are "known" by EF and are reflected in results.
But the updated records are not read from database and old values are kept from local cache.
For me , It's ridiculous that EF does not have a command to "READ ALL THE DATA AGAIN FROM DATABASE"
|
|
|
|
|
Those changes are NOT known by EF. At least not until you run a query again to retrieve the entire set of records again. But now you have a problem. How does EF know that the records are deleted? It doesn't. The records just don't show up in the result set from the database. That in no way means that they were deleted. The only way to know that would be to get the record ID of what EF has cached and try to retrieve that record ID from the database. Now do that for every record you have cached and it'll take a long time.
Move on to the problem of just telling EF to reload a specific table. Great, you now have all of the data from that table, but how to do handle the case where the cached data is "dirty"? That means you have data in the cache that's been modified but not written back tot he database. Do you through those changes out in favor of the current data? Do you skip that changes? Do you modify only certain fields? How is the ORM supposed to know which fields to keep and which to throw out and refresh?
Another problem. What query returned the data the generated the object in the cache? There is ORM that I'm aware of that caches the query that generated a set of object graphs. What if there's multiple queries that can be run that generate a particular object in a graph? How do you know which query to run to PROPERLY regenerate that object with the correct data? It's impossible for the ORM to know that.
Google "database concurrency" for the difficulties in doing what you want to do. There is no easy solution to this problem, and not every solution is going to work for every application.
You're looking at this through the lens of what you want EF to do and not from the standpoint of how any ORM would even accomplish this in a generic way usable by all applications. I know you just want to call .Reload() and have it work, but you're not seeing the complexity behind actually making that happen.
|
|
|
|
|
Quote: How does EF know that the records are deleted? It doesn't. The records just don't show up in the result set from the database.
Ok , and why EF does not keep the records that are in local cache , but instead "use the information" that those records are not show up in the result set of database ? And why in the case of modified records , "the information from database" is not used , and it's just keep the records from local cache.
Quote: Move on to the problem of just telling EF to reload a specific table. Great, you now have all of the data from that table, but how to do handle the case where the cached data is "dirty"?
If the cached data is modified , i think EF should let the programmers to choose . If i try to reload data from database when cached data is modified , then i'm responsible to get the new data from database and overwrite those changes. But In my application for example , if a modification was made , i don't let the users to reload data from database. It's simple. And how you will resolve the problem when cached data is modified and i create a new context ( as many have suggested this idea for my problem). This is the same problem. I think these problems EF should leave in the hand of programmers.
|
|
|
|
|
Quote: Ok , and why EF does not keep the records that are in local cache , but instead "use the information" that those records are not show up in the result set of database ? And why in the case of modified records , "the information from database" is not used , and it's just keep the records from local cache.
That made no sense at all.
EF maintains a cache of objects reconstituted from result sets returned by the database. Once the result set is returned, there is no connection between the objects and the database. If the database changes, EF gets no notification at all that anything happened to the database copy.
If you have modified records, EF maintains the original "as loaded" copy of the object and the modifications to it. When EF goes to update the records in the tables, all of the original field values go into the WHERE clause of the SQL DELETE OR UPDATE statements. If any of those fields do not match the current data in the fields in the database table, the query fails to update the record because of a concurrency problem. You're not updating the latest version of the record. You're trying to update a newer version of the record than what you retrieved. That is a problem not solved by EF or the database.
Seriously, go Google "database concurrency" and start reading.
desanti wrote: If the cached data is modified , i think EF should let the programmers to choose
And how do you propose you do that in the EF code? EF avoids the problem by not handling it at all, letting YOU decide how to handle it, which is why you even asked this question in the first place because of performance. Having EF do it internally will not make it any faster. It still has to same limitations you do handling it yourself, row-by-row from the cached data.
There is no solution to the performance problem! Again, your only chance at making either of the two solutions "faster" is to cache less data at the client.
|
|
|
|
|
Quote: And how do you propose you do that in the EF code? EF avoids the problem by not handling it at all, letting YOU decide how to handle it, which is why you even asked this question in the first place because of performance. Having EF do it internally will not make it any faster. It still has to same limitations you do handling it yourself, row-by-row from the cached data.
With "Letting in the hand of programmers" , i mean informing them some rules , that if the local data is modified and they try to load from database , the local cache will be overwritten. So before loading from database , save or Undo those changes , but if you want to do without save or undo , you will loose those changes. I think is simple . The police does not close the road for people because there are cars moving on it. But there are semaphores and people are informed that when the semaphore is green the can walk safely, if is red they should not walk but they are free to do , keeping the risk by itself.
|
|
|
|
|
desanti wrote: hat if the local data is modified and they try to load from database , the local cache will be overwritten
So is that behavior desirable in all applications and situations? Nope.
So how do you configure that behavior in code, and all possible variants? Like either keeping the modified data, dumping all the modified data, or just keeping some of it and replacing the rest, or replacing some of it based on rules, or dumping some of it based on rules, ... there's too many possibilities to list... And that's the exact reason why it doesn't show up in ORM's.
You're trying to get EF, and ORM's in general, to do something that is best left up to the coder. Hell, that's even what you said in your previous post!
By the way, your analogy doesn't even apply to this.
|
|
|
|
|
Quote: So how do you configure that behavior in code, and all possible variants? Like either keeping the modified data, dumping all the modified data, or just keeping some of it and replacing the rest, or replacing some of it based on rules, or dumping some of it based on rules, ... there's too many possibilities to list
no no , i'm telling that EF could have some rules , for example if you load data from database ( with a special command like reload ) the cached data will be overwritten. So a programmer can use this and i think it's easy to resolve the situations.EF has a Reload command but for a single object at time . If a have a list of entries that i want to reload , i should do one by one ??? why not all with a single command !!!!
By your opinion , better to have a full working Reload command and let programmers to resolve the possibilities . or to have something like my second solution where i'm fooling EF telling that those objects are detached in order to resolve my problem ????!!!
|
|
|
|
|
Sorry , I found a new way :
Dim contextobj = (CType(context, IObjectContextAdapter)).ObjectContext
contextobj.Refresh(RefreshMode.StoreWins, context.myobjs.Local)
contextobj.Refresh(RefreshMode.StoreWins, context.mychilds.Local)
Is there anything wrong with this method ?
modified 4-Apr-19 8:31am.
|
|
|
|
|
Technically, no.
As far as your business rules go, you're the only one who would know.
|
|
|
|
|
Then , really I don't understand why a such method does not exist on DBContext directly but I should use objectContext.
Because the option StoreWins means that the local cache will be overwritten with new data on my database , we have discussed this on our conversation.
|
|
|
|
|
I've already said my piece. I'm out.
|
|
|
|
|
hi everyone
i am new in developpement vb.net so i ask if i can import a text file to a datagridview i want a datatable because i want after to add a new column that came from database
please help
|
|
|
|
|
You could import a text file directly to a datatable without storing it in a database.
However, the basic steps you need to perform:
1) Create a database table with columns defined to match the type of data that you want from the text file. Ex: Integer, date, Varchar, etc
2) create your VB program to loop through the text file parsing the data into the columns you want to store.
3) Execute a database INSERT statement to load the data on a line by line basis.
As you can see, this is a very vague solution. What area do you need help with?
Do you know how to create a database table? CREATE TABLE
Do you know know how to write a program to read a text file line by line?
Do you know how to parse a line into various fields?
Do you know how to execute an INSERT statement ?
Your question seems to be as vague as my proposed solution. Post a specific question so the group can answer it.
Good Luck.
|
|
|
|
|
i know all of that,my problem is how to manage to import ths txt file and data from database to a single datagridview
|
|
|
|
|
|
but this code is written in c not vb
|
|
|
|
|
It's C#, and the actual code snipppets are small enough that you can easily convert it VB.NET.
|
|
|
|
|
ok thanks i will make a try
|
|
|
|
|
Message Removed
modified 5-Apr-19 3:56am.
|
|
|
|
|
I'm trying to add a custom tooltip to my datagridview(s). In my research I know that the System.Windows.Forms.DataGridViewCellMouseEventArgs has no mouse hover event so instead I am using System.EventArgs. My problem of course that the mouse hover event only fires once on the datagridview until the user moves the mouse off the control. I need the event to fire each time the user hovers over a new row. Is there a way to reset the hover event on mouse move within the same control? I have searched and cannot find anything on this.
Here is my code which works perfectly but as mentioned only fires once.
Private Sub dtgNewMrs_cellMouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles dtgNewMRs0.MouseHover, dtgNewMRs1.MouseHover, dtgNewMRs2.MouseHover, dtgNewMRs3.MouseHover, dtgNewMRs4.MouseHover, dtgNewMRs5.MouseHover
Dim remMRNum As String
Dim remMRLin As String
Dim tTip As String = Nothing
Dim tCount As Integer = 1
Dim grvScreenLocation As Point = sender.PointToScreen(sender.Location)
Dim tempX As Integer = DataGridView.MousePosition.X - grvScreenLocation.X + sender.Left
Dim tempY As Integer = DataGridView.MousePosition.Y - grvScreenLocation.Y + sender.Top
Dim hit As DataGridView.HitTestInfo = sender.HitTest(tempX, tempY)
Dim chkRemarks As DataTable = rSet.Tables("RemarksMR")
With sender
remMRNum = .Rows(hit.RowIndex).Cells(3).Value
remMRLin = .Rows(hit.RowIndex).Cells(4).Value
Dim tQry = (From rems In chkRemarks.AsEnumerable()
Where rems.Field(Of String)("MR_Num") = remMRNum And
rems.Field(Of String)("MR_Line") = remMRLin).ToList()
Dim r = From t In tQry
Select t
If r.Count > 0 Then
For Each row As DataRow In r
tTip = tTip & Str(tCount) & ". " & row.Item(3) & Chr(13)
tCount += 1
Next
End If
End With
remTip.SetToolTip(sender, tTip)
|
|
|
|
|