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.
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 ?
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).
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).
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.
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.
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.
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.
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.
Member
90 Points
520 Posts
Visual Studio 2019 can not catch sql errors
Jul 29, 2019 10:16 PM|fatihbarut|LINK
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?
All-Star
43741 Points
18714 Posts
Re: Visual Studio 2019 can not catch sql errors
Jul 30, 2019 12:39 AM|mgebhard|LINK
Can you share code that reproduces the error?
Member
90 Points
520 Posts
Re: Visual Studio 2019 can not catch sql errors
Jul 30, 2019 06:53 AM|fatihbarut|LINK
sure
if (_context.Evraklar.SingleOrDefault(e => e.GonderiId == GonderiNo).GonderiId == GonderiNo)
{
return "BuGonderiGirilmis";
}
All-Star
45840 Points
16682 Posts
Re: Visual Studio 2019 can not catch sql errors
Jul 30, 2019 07:20 AM|PatriceSc|LINK
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.
Participant
1780 Points
576 Posts
Re: Visual Studio 2019 can not catch sql errors
Jul 30, 2019 07:51 AM|Yang Shen|LINK
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
Member
90 Points
520 Posts
Re: Visual Studio 2019 can not catch sql errors
Jul 30, 2019 09:54 AM|fatihbarut|LINK
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?
All-Star
45840 Points
16682 Posts
Re: Visual Studio 2019 can not catch sql errors
Jul 30, 2019 10:07 AM|PatriceSc|LINK
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:
All-Star
43741 Points
18714 Posts
Re: Visual Studio 2019 can not catch sql errors
Jul 30, 2019 10:53 AM|mgebhard|LINK
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.
Member
90 Points
520 Posts
Re: Visual Studio 2019 can not catch sql errors
Jul 30, 2019 11:22 AM|fatihbarut|LINK
thanks for the .Any I was actually looking for it :)
Member
90 Points
520 Posts
Re: Visual Studio 2019 can not catch sql errors
Jul 31, 2019 07:49 AM|fatihbarut|LINK
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?
All-Star
45840 Points
16682 Posts
Re: Visual Studio 2019 can not catch sql errors
Jul 31, 2019 08:04 AM|PatriceSc|LINK
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).
Member
90 Points
520 Posts
Re: Visual Studio 2019 can not catch sql errors
Jul 31, 2019 08:07 AM|fatihbarut|LINK
thanks a lot you are great!
I was obviously in release mod.
All-Star
45840 Points
16682 Posts
Re: Visual Studio 2019 can not catch sql errors
Jul 31, 2019 08:09 AM|PatriceSc|LINK
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).
Member
90 Points
520 Posts
Re: Visual Studio 2019 can not catch sql errors
Jul 31, 2019 01:19 PM|fatihbarut|LINK
do you know what you are right, I am in debug mode and still cant catch the error
Member
90 Points
520 Posts
Re: Visual Studio 2019 can not catch sql errors
Jul 31, 2019 02:34 PM|fatihbarut|LINK
how can I add sql exceptions into the exceptions list?
All-Star
43741 Points
18714 Posts
Re: Visual Studio 2019 can not catch sql errors
Jul 31, 2019 02:50 PM|mgebhard|LINK
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.
Member
90 Points
520 Posts
Re: Visual Studio 2019 can not catch sql errors
Jul 31, 2019 02:52 PM|fatihbarut|LINK
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
All-Star
43741 Points
18714 Posts
Re: Visual Studio 2019 can not catch sql errors
Jul 31, 2019 02:59 PM|mgebhard|LINK
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.
Member
90 Points
520 Posts
Re: Visual Studio 2019 can not catch sql errors
Aug 07, 2019 06:08 PM|fatihbarut|LINK
btw Any() doesn't work with ef core and obviously, the reason is not me.
All-Star
43741 Points
18714 Posts
Re: Visual Studio 2019 can not catch sql errors
Aug 07, 2019 06:28 PM|mgebhard|LINK
You're making assumptions again...
Any() is a LINQ extension. Add...
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.
Member
90 Points
520 Posts
Re: Visual Studio 2019 can not catch sql errors
Aug 07, 2019 07:31 PM|fatihbarut|LINK
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...
All-Star
43741 Points
18714 Posts
Re: Visual Studio 2019 can not catch sql errors
Aug 07, 2019 07:55 PM|mgebhard|LINK
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
All-Star
45840 Points
16682 Posts
Re: Visual Studio 2019 can not catch sql errors
Aug 07, 2019 08:37 PM|PatriceSc|LINK
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:
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.