entity framework not updating query after upgrading solution, provider reference - c#

I recently upgraded a project from VS2012 to VS2013 (it was originally created in 2010) and also upgraded the reference to one of the database providers (Pervasive.Data.Common, Pervasive.Data.SqlClient, Pervasive.Data.SqlClient.Entity) to the newer 4.0 version. On doing this, two lines of code that were previously working no longer properly refresh the data in their queries, which are inside a using statement that is inside a function that is called each time, so there should be no long-lived objects, including both contexts and entity objects (all other statements work properly.) The code lines at issue attempt to pull a single purchase order header tuple from the po table and a single purchase order detail line item tuple from the po_dtl table. Placing a context.refresh call at the beginning of the code inside the using statement causes the data to properly refresh, but is a serious performance killer and won't work for production, not to mention that there should be no need to refresh, EF should be running the query anew each time the function is called.
Below is a portion of the code with the meat of the function left out for clarity. I have also left out the po_dtl portion of the code, as the queries to determine which po_dtl tuple to use are complex and would make the code hard to read. The line of code that is failing is noted by a comment saying so.
I have stepped through the code multiple times and have verified that
the context object always APPEARS to be brand new at the beginning of the statement, having no tracked entities
the value of poNum (the id of the po header being looked up) changes as expected
no matter what value poNum has, the po header that was returned by the very first execution of the query is always returned by this line of code
public ReceivePieceInfo(AdvanceShippingNoticePiece item, int profitUserId, Inventory.Location receiveLocation, DateTime receiveDate, string tripNumber)
{
// LinqContextMaster.NewProfitContext provides a brand new
// context each time it is called
using (Profit.ProfitDatabase pd = LinqContextMaster.NewProfitContext)
{
decimal poNum = decimal.Parse(item.PurchaseOrderNumber);
decimal poLineNum = decimal.Parse(item.PurchaseOrderLineNumber);
IEnumerable<Profit.po> profit_pos = null;
// THIS IS THE BROKEN CODE - no matter what poNum is, this
// query always returns the very first PO that was queried
profit_pos = pd.po.Where(po => po.po_no == poNum);
Profit.po profit_po = null;
if (profit_pos.Count() == 1)
profit_po = profit_pos.Single();
}
}

Related

Cannot select from the MySQL view with EF

I had a complex view joining a couple of tables but it freezed my C# app so I simplified it in order to get it to work, however it still freezes and I dont have a clue why.
I have a table called 'admittances' containing: AdmittanceID, Number, Date, Notes.
I created a view which selects three of those: AdmittanceID as ID, Number as Number and Date as Date.
In a database it just works, no big deal. I mapped it via 'Update model from database' option in a model wizard inside C# EF model and it shows up as an entity with three columns and entity key column 'ID'.
However while trying to loop over all the records the program stops working and does not go any further (in debugging it just does not pass through the loop).
meraserwEntities db = new meraserwEntities();
var myQuery = db.clientstodevices.ToList();
//Tried also other versions that are listed below
var myQuery = from d in db.clientstodevices select d;
foreach (var item in myQuery)
{
ClientsToDevices.Add(item);
}
// Or without loop - then it freezed exactly in this line
ClientsToDevices = db.clientstodevices.ToList();
Please help, tried everything ..

Simple LINQ query gets NullReferenceException even though SQL Server table has data

I have a simple Linq query that is failing suddenly. This worked up until yesterday and I have not been able to determine the problem.
The query is simply getting a complete view from SQL Server
using(JobEntities JE = new JobEntities())
{
var BatchData = (from x in JE.vwBatchObjectActiveBatches select x).ToList();
}
Starting yesterday this line gets a
NullReferenceException (Object reference not set to an instance of an object)
My suspicion was that a user put in bad data causing the view to fail on SQL Server, but I have checked SQL Server itself and the view runs fine and populates with data.
This query was running in the middle of a larger function loading data from many places, so I have created a test case where I simply load the main window and run this query directly in the code behind to make sure that nothing else is affecting it, and the query still fails. All other Linq queries that I run in this project work still, only this one fails. The app is not under any production right now, and has been static for several months at least.
When I look at the JE in the watch window I can see the vwBatchObjectActiveBatches and it lists 164 records in the Local section -- this matches the view results on SQL Server. Expanding the Results View shows the null error again.
How can I find and fix whatever is causing this query to fail? Why does the results view show an error but the local Line shows the data that I am attempting to get?
It looks like your database returns NULL where Entity Framework does not expect/allow it. Data returned should be in accordance with the definition of its datamodel objects.
Solution: either 'fix' the data, or fix the query that produces it, or change the definition of your datamodel objects to allow NULL for the conflicting field(s).

EF6: Check if previous DB entry is the same as new object

This is C# and EF6. I have a function that sometimes runs twice (by design). As part of that function I'd like to check if a new object is the same as the newest entry in the DB and was created within the past 3 seconds. The main problem is that the Id column is auto-filled when the item is fetched, and EF6 won't let me clear it or set it to a standard number since it's the key property.
I think I can figure out the logic to check the time, but what would be the correct way to check if the two items are otherwise the same?

Does SQL CE ExecuteResultSet need to be cleaned up?

I have a table with about 150 rows in it. These are the data types in the table:
int (PK)
nvarchar(52)
tinyint
datetime
uniqueidentifier
nvarchar(300)
bit
bit
bit
bit
I download the data from a web service and insert into the database. When I do that it works fine.
I later in the execution of my program I call the web service again. Since I may have updated some of the data I downloaded the first time, I check the db to see the row has changed. If it has then I leave it, if not then I update it. I think it is the check to see if it is there that is causing me the problems. When I do it I get this error:
"SQL Server Compact has exceeded the buffer size. The default size can be increased on initialization by modifying the ssce: max buffer size property. [ The default size = 655360 ]"
NOTE: This does not happen right away on the second time around. (Meaning that I stepped through some rows and they updated just fine.)
The only thing I can think of is that my result set is not getting cleared out. (Though I have used the same code to access the database with no problems.)
Here is my code:
public static SqlCeResultSet SetupTable(string tableName, string indexName,
bool allowUpdates, params object[] whereValues)
{
// The command used to affect the data
var command = new SqlCeCommand
{
CommandType = CommandType.TableDirect,
Connection = _connection,
// Set the table that we are going to be working with.
CommandText = tableName,
// Indicate what index we are going to be using.
IndexName = indexName
};
if ((whereValues != null) && (whereValues.Length > 0))
command.SetRange(DbRangeOptions.Match, whereValues, null);
// Get the table ready to work with.
if (allowUpdates)
return command.ExecuteResultSet(
ResultSetOptions.Updatable | ResultSetOptions.Scrollable);
else
return command.ExecuteResultSet(ResultSetOptions.Scrollable);
}
The call looks something like this:
SetupTable("tblMyTable", "IndexName", true, whereValue);
The weird thing is that it all works fine if I don't use the SetRange. It seems to me that it should use less buffer space if I use a SetRange (not more as it seems to be doing).
After it crashes with this error calls in Query Analyzer will also give the same message. I could up my buffer size but I am sure it will just take a bit longer to fill up (especally because I am passing in a "where" value that sets the range to a single row).
One thing to note is that I call the above code for each row in my table. (That is why I am asking if I should be cleaning up my results.) While I do call it for each row in my table, the previous one goes out of scope before I make a new one.
Any help would be great!
(Note: if you want to see the full code for the SetupTable stuff I put the whole class here.)
Are you disposing your command somewhere?
Anything that implements IDisposable (has a Dispose() method) should be disposed. That's the general rule. Since you're calling this method for every row, you should dispose your command.

LINQ to SQL "1 of 2 Updates failed" on "SubmitChanges()"

I am updating an object of type X and its children Y using LINQ to SQL and then submitting changes and getting this error
Example Code
X objX = _context.X.ToList().Where(x => x.DeletedOn == null).First();
objX.DeletedOn = DateTime.Now;
EntitySet<Y> objYs = objX.Ys;
Y objY = objYs[0];
objY.DeletedOn = DateTime.Now;
_context.SubmitChanges();
On SubmitChanges() I get an exception "1 of 2 Updates failed", no other information as to why that happened. Any ideas?
Also the exception type is
ChangeConflictException
Sooo what was the cause of the problem
- A trigger
I did a sql profiler and saw that
When ObjY's DeletedOn property got updated a trigger updated
ObjX's property (value in table) called CountOfX
which led to an error as the SQL created by LINQ to SQL had the old CountOfX value in it.
Hence the conflict.
If you ever get this error - SQL profiler is the best place to start your investigation
ALSO NOT RELATED TO THE QUESTION
I am testing LINQ to SQL and ADO.net Framework, weirdly this error happened in LINQ to SQL but not in ADO.net framework. But I like LINQ to SQL for its Lazy Loading. Waiting for EF to get outta beta
I'm not sure what the cause of the error may be exactly, but there seem to be a number of problems with the example you've provided.
Using ToList() before the Where() method would cause your context to read the entire table from the DB into memory, convert it to an array; and then in the same line you immediately call Where which will discard the rows you've loaded, but don't need. Why not just:
_context.X.Where(...
The Where method will return multiple items, but the second line in the example doesn't appear to be iterating through each item individually. It appears to be setting the DeletedOn property for the collection itself, but the collection wouldn't have such a property. It should fail right there.
You are using DateTime.Now twice in the code. Not a problem, except that this will produce ever so slightly different date values each time it is called. You should call DateTime.Now once and assign the result to a variable so that everything you use it on gets identical values.
At the point where you have "Y objY = objYs[0]" it will fail if there are no items in the Y collection for any given X. You'd get an index out of bounds exception on the array.
So given this example, I'm not sure if anyone could speculate as to why code modeled after this example might be breaking.
In LINQ2SQL Data Context diagram select the Entity and the field where the count is stored. (A denormalized figure)
Now set the UpdateCheck = Never.
I had this kind of issue. I was debugging running single lines at a time. It turned out another process was modifying this record.
My manual debugging process was slowing down the normal speed of the function. When I ran it all the way to a line after the SubmitChanges method, it succeeded.
My scenario would be less common, but the nature of this error relates to the record becoming superceded by another function/process. In my case it was another process.

Categories