Query execution did not find a matching element

13 posts, 0 answers
  1. Duncan
    Duncan avatar
    93 posts
    Member since:
    Jul 2012

    Posted 17 Oct 2011 Link to this post

    I am getting this error: Query execution did not find a matching element. Yet i know for a fact that there is a matching value in the DB.

    Also the query does work most of the time, and especially if there is a breakpoint in the code somewhere. If i let the code run freely, this is when the error is produced.

    The particular line of code is:

    ...
    trig.States = dbContext.States.Single(s => s.StateCode == trigger.VisitorState.StateCode);
    ...
    dbContext.Add(trig);
    }
    ...
    dbContext.SaveChanges();

    Does anyone know why this is?

    Duncan
  2. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 18 Oct 2011 Link to this post

    Hello Duncan,

    please give us a bit more information:
    (a) which CLR type is StateCode
    (b) what column type does StateCode have
    (c) is StateCode the primary key
    (d) can there be a upper/lower case issue?

    Best wishes,
    Thomas
    the Telerik team

    Check out the latest stable build of Telerik OpenAccess ORM. Download it and benefit from our new Project Templates.

  3. DevCraft banner
  4. Duncan
    Duncan avatar
    93 posts
    Member since:
    Jul 2012

    Posted 18 Oct 2011 Link to this post

    a: String

    b: nvarchar(50) - However, i have changed this to nvarchar(MAX) as i was noticing some blank characters that needed to be trimed

    c: No, it is (most of the time) 2 characted code like CA, AZ, FL, etc...

    d: No, and even the blank spaces should nto have been a issue as i was doing a lookup for the value out of the DB and then later this exact value out of the DB was being used in this query, so blank spaces or case should have been exact to what was in the DB...
  5. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 19 Oct 2011 Link to this post

    Hi Duncan,

    ok, that means we have a situation where the query is executed against the database (no pk shortcut taken) and sometimes fails to find an instance that seems to have been there before. Now, I have more questions:
    (a) Which database do you use.
    (b) Please check that the column type in the model for State is correctly set to nvarchar(max).
    (c) I would really like to understand why debugging should have an influence on the result. The only thing that I think could play a role here is the automatic display of property values in the debugger, as the display will need to fetch them from the database, thus altering the internal state compared to non-debugging executions. Can you turn off this feature in the Visual Studio (tools/debugging) and try again?

    Kind regards,
    Thomas
    the Telerik team

    Check out the latest stable build of Telerik OpenAccess ORM. Download it and benefit from our new Project Templates.

  6. Caleb
    Caleb avatar
    10 posts
    Member since:
    Nov 2011

    Posted 08 Nov 2011 Link to this post

    I am getting this same error.
    "Query execution did not find a matching element."  I have called

    if (scope.Transaction.IsActive == false)
            {
                scope.Transaction.Begin();
                Genotype genotype = (from g in scope.Extent<Genotype>() where g.IdGenotype == id select g).First();

    }

    I am getting the error on the bold line and I can not see what could be wrong
  7. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 08 Nov 2011 Link to this post

    Hi Caleb,

    this exception occurs, when there is no instance with that id. In case you want to get a null back instead of the exception, you will need to use FirstOrDefault().

    Greetings,
    Thomas
    the Telerik team

    NEW and UPDATED OpenAccess ORM Resources. Check them out!

  8. Caleb
    Caleb avatar
    10 posts
    Member since:
    Nov 2011

    Posted 08 Nov 2011 Link to this post

    Thank you Tom fro your response it has been very helpful but I know that record exists....  What other issues could exist?  
  9. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 08 Nov 2011 Link to this post

    Hello Caleb,

    is the instance locked on the database, are there parallel transactions running? What is the mapping of the field and what is the field type?

    Best wishes,
    Thomas
    the Telerik team

    NEW and UPDATED OpenAccess ORM Resources. Check them out!

  10. Duncan
    Duncan avatar
    93 posts
    Member since:
    Jul 2012

    Posted 08 Nov 2011 Link to this post

    Caleb,

    My issue was a difference in the database column vs entity type. I can't recall the exact types unfortunatly but it was something silly like my DB column was nchar(10) and after switching it to nvarchar(MAX) the problem seemed to go away.

    I can dig into this further for you if that does not help.

    Duncan
  11. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 10 Nov 2011 Link to this post

    Hello Caleb,

    is this a nvarchar/varchar issue? Again, can you give us this information; we would like to rule this out as a problem before releasing a new version.

    Regards,
    Thomas
    the Telerik team

    NEW and UPDATED OpenAccess ORM Resources. Check them out!

  12. Caleb
    Caleb avatar
    10 posts
    Member since:
    Nov 2011

    Posted 10 Nov 2011 Link to this post

    Well guys you where right with the first issue.  Come to find out the query was not the same as the class and DB.

    CD
  13. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 11 Nov 2011 Link to this post

    Hello Caleb,

    so the issue is resolved for you now? And the reason was that the class/table information did not match what you had in the database? Was there a manual change made by you to the mapping or did we read the database tables incorrecty? If the latter is the case, what kind of table definition did you have?

    All the best,
    Thomas
    the Telerik team

    NEW and UPDATED OpenAccess ORM Resources. Check them out!

  14. Ian
    Ian avatar
    26 posts
    Member since:
    Jun 2012

    Posted 23 Apr 2012 Link to this post

    I think the paragraph
    "
    My issue was a difference in the database column vs entity type. I can't recall the exact types unfortunatly but it was something silly like my DB column was nchar(10) and after switching it to nvarchar(MAX) the problem seemed to go away.
    "
    points out the issue.  The column was a fixed width column - nchar(10).  So even if it was only fed 3 characters "ABC" there would still be "ABC......." (dot for space) in the column.  A LIKE/StartsWith query would have worked, or using a varchar/nvarchar column would also have worked. 
    I tend to avoid the fixed width columns for this very reason.  I often have to deal with it when we read data into our SQL database from FoxPro and other DB systems.  Storing the data in varchar columns and using RTRIM() makes things tidy.
Back to Top
DevCraft banner