locked
Visual Studio 2019 can not catch sql errors RRS feed

  • Question

  • User283528319 posted

    hi all,

    while VS 2017 gives this error "Sequence contains more than one element'" for singleordefault query VS 2019 says nothing

    what can be the reason behind it?

    Monday, July 29, 2019 10:16 PM

All replies

  • User475983607 posted

    while VS 2017 gives this error "Sequence contains more than one element'" for singleordefault query VS 2019 says nothing

    what can be the reason behind it?

    Can you share code that reproduces the error?

    Tuesday, July 30, 2019 12:39 AM
  • User283528319 posted

    sure

    if (_context.Evraklar.SingleOrDefault(e => e.GonderiId == GonderiNo).GonderiId == GonderiNo)
    {
    return "BuGonderiGirilmis";
    }

    Tuesday, July 30, 2019 6:53 AM
  • User753101303 posted

    Hi

    And which value is returned by _context.Evraklar.Count(e => e.GonderiId == GonderiNo) ?

    GonderId is not supposed to be a unique id ? Your code won't work if no row is found and testing that the returned object does match the criteria you used to retrieve it seems weird.

    Tuesday, July 30, 2019 7:20 AM
  • User-719153870 posted

    Hi fatihbarut,

    I tested singleordefault method in vs2017 and vs2019, both of them output the same error message.

    In VS2017:

    In VS2019:

    Is it possible that you have another error in your code that vs2019 prompted another error message instead of this?

    Best Regard,

    Yang Shen

    Tuesday, July 30, 2019 7:51 AM
  • User283528319 posted

    no, but is it possible because of extensions?

    vs 2017 has over 20 extensions in my machine. vs 2019 has 7-8

    maybe 2019 has web based extension lack.

    is it possible?

    Tuesday, July 30, 2019 9:54 AM
  • User753101303 posted

    You could use "go to definition" to check this is the usual extension method. Are you 100% sure you don't just get one row ? It takes few seconds to check but at least you are sure it just doesn't work as expected. I never saw or even heard someone telling that SingleOrDefault doesn't work as expected.

    You are using EF 6 with SQL Server ? Or maybe the beta EF 6.3 version ?

    Edit: or maybe you changed exception settings ?? https://docs.microsoft.com/en-us/visualstudio/debugger/managing-exceptions-with-the-debugger?view=vs-2019

    I would really try to "close doors" first by checking the count and testing for example:

    var test= new string[]{ "A", "B" };
    var value = test.SingleOrDefault(); // Should throw
    



    Tuesday, July 30, 2019 10:07 AM
  • User475983607 posted

    no, but is it possible because of extensions?

    vs 2017 has over 20 extensions in my machine. vs 2019 has 7-8

    maybe 2019 has web based extension lack.

    is it possible?

    The error message is thrown by the SingleOrDefault() extension method during run-time.  The reference documentation explain the possible errors.

    https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.singleordefault?view=netframework-4.8

    Visual Studio is an editor and extensions have nothing to do with the run-time exception.

    Lastly, refactor your code.

    if (_context.Evraklar.Any(e => e.GonderiId == GonderiNo))
    {
    	return "BuGonderiGirilmis";
    }

    Tuesday, July 30, 2019 10:53 AM
  • User283528319 posted

    thanks for the .Any I was actually looking for it :)

    Tuesday, July 30, 2019 11:22 AM
  • User283528319 posted

    I have both VS 2017 and VS 2019 installed in my machine and as far as I know they share some settings files.

    can this be the problem?

    Wednesday, July 31, 2019 7:49 AM
  • User753101303 posted

    No they are not supposed to. Rather than trying to guess always try to see what actualkly happens ie here :
    - as I told previously make 100% sure you are supposed to have an exception (ie start by showing how many rows are returned)
    - if try catch shows you do have one while it is ignored in VS, check maybe "exception settings" in VS

    Or maye you are in release mode and your own code does ignore them? (but don't guess this, see this, in general don't try to just guess but conduct "experiments" which most often allows to narrow down quickly the situation).

    Wednesday, July 31, 2019 8:04 AM
  • User283528319 posted

    thanks a lot you are great!

    I was obviously in release mod.

    Wednesday, July 31, 2019 8:07 AM
  • User753101303 posted

    Still your app should be able to report exceptions (usually a short general message to the user and you log details somewhere IT support can see and check them). 

    Wednesday, July 31, 2019 8:09 AM
  • User283528319 posted

    do you know what you are right, I am in debug mode and still cant catch the error

    Wednesday, July 31, 2019 1:19 PM
  • User283528319 posted

    how can I add sql exceptions into the exceptions list?

    Wednesday, July 31, 2019 2:34 PM
  • User475983607 posted

    how can I add sql exceptions into the exceptions list?

    Exceptions are thrown by code.  This including SQL Exceptions.  My best guess is you wrote code to catch an exception and you are expecting the exception to bubble up.  If you want exception to bubble to the surface (or whatever layer) you need to rethrow the exception.

    Wednesday, July 31, 2019 2:50 PM
  • User283528319 posted

    interesting but look at here, there is a difference between VS 2017 and VS 2019 installations called red gate SQL search, is it cause of the problem?

    https://screenshot.net/tr/lxm92h7

    redgate

    Wednesday, July 31, 2019 2:52 PM
  • User475983607 posted

    interesting but look at here, there is a difference between VS 2017 and VS 2019 installations called red gate SQL search, is it cause of the problem?

    https://screenshot.net/tr/lxm92h7

    redgate

    You're making wild assumptions.

    SQL Search is a plugin for Visual Studio that allows you to search your DB for a a string.  It returns tables, views, and procs that contain the search parameter.

    Wednesday, July 31, 2019 2:59 PM
  • User283528319 posted

    Lastly, refactor your code.

    btw Any() doesn't work with ef core and obviously, the reason is not me.

    Wednesday, August 7, 2019 6:08 PM
  • User475983607 posted

    btw Any() doesn't work with ef core and obviously, the reason is not me.

    You're making assumptions again...

    Any() is a LINQ extension.  Add...

    using System.Linq;

    to the top of the cs file.

    Keep in mind, Visual Studio provides a helper (light bulb) when you place the cursor over errors.  If you click the light bulb it will show a list of fixes and you can pick one form the list.

    Wednesday, August 7, 2019 6:28 PM
  • User283528319 posted

    You're making assumptions again...

    no... you are doing assumptions. I already have system.linq in my uses

    why don't you search internet about this subject. Any() really doesn't work with EF core...

    Wednesday, August 7, 2019 7:31 PM
  • User475983607 posted

    fatihbarut

    why don't you search internet about this subject. Any() really doesn't work with EF core...

    I use Any() with EF Core so I know it works.  My best guess it you do not understand how Any() works.  Perhaps you made an assumption without reading the reference docs.  Anyway, Any() returns true or false and why I suggested Any() as you are using it in a conditional statement.  Any() does not return an Entity of that's what you are assuming.

    https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.any?view=netframework-4.8

    Wednesday, August 7, 2019 7:55 PM
  • User753101303 posted

    With EF Core and the SQL Server provider or you are using MySQL. What if you try in a new project with a new databasse something such as:

                using (var db =new Db())
                {
                    if (!db.Items.Any())
                    {
                        db.Items.Add(new Item { Name = "A" });
                        db.Items.Add(new Item { Name = "A" });
                        db.SaveChanges();
                    }
                    else
                    {
    var count = db.Items.Count(o => o.Name == "A"); // To make 100% if SingleOrDefault is supposed to throw var item = db.Items.SingleOrDefault(o => o.Name == "A"); } }

    it works fine here ie on the first run it add two items and on the next run it fails with an exception. As I asked multiple times I also count how many items I'll found so that I can make sure SingleOrDefault is supposed to thrown.

    Wednesday, August 7, 2019 8:37 PM