Quantcast
Channel: Visual Studio and Visual Web Developer Express
Viewing all articles
Browse latest Browse all 3509

Unexpected Behavior in Visual Studio Ultimate 2015 Debugger - Am I doing something wrong as a C# Programmer?

$
0
0

Here's my code snippet:

ApplicationDbContext context =
                new ApplicationDbContext();
System.Data.Entity.Infrastructure.DbQuery<ApplicationUser> DbQuery1 = ((System.Data.Entity.Infrastructure.DbQuery<ApplicationUser>)context.Users); DbQuery1 = DbQuery1.Include("Address");

The "context.Users" property is declared as a System.Data.Entity.IDbSet<ApplicationUser> type in the class it resides in. That means it can interface with anactual System.Data.Entity.DbSet<ApplicationUser> type.

Well, at runtime that's what type context.Users actually is. It is a : System.Data.Entity.DbSet<ApplicationUser> type.

And System.Data.Entity.DbSet<> is simply a derivative of System.Data.Entity.Infrastructure.DbQuery<>.

And since I need access to the "Include" method of the System.Data.Entity.Infrastructure.DbQuery<> class, that is the reason for all this madness. Okay I am being over dramatic. That is the reason for my unusual cast.

But after executing the very last line of my code snippet above, in VS 2015 Ultimate's Debugger, it goes bananas and jumps straight out of my code into another completely unrelated method. I guess I shouldn't say completely unrelated. It jumps to another method in my MVC application. This suggests to me that I am not using references in a proper manner. Can somebody please explain to me what I think I am doing with my code snippet above and what its actually doing and why I shouldn't be doing these types of casts?

Or is my code acceptable? Please give guidance. Thank You.

P.S. To the moderators of this post. A more appropriate thread for me to have chosen might have been. MVC, Entity Framework, or C#. Or maybe it would pass as appropriate for all 3. I don't know. I'll leave it up to you.

Edit: More research. When I set a breakpoint at the 3rd line of code above and go to the "Command" window and execute "? DbQuery1"  I get this: "error CS0103: The name 'DbQuery1' does not exist in the current context".

Strangely enough, at this same breakpoint, issuing this command on the command line confirms that a subsequent declaration of an ApplicationUser is already in scope and has valid values. Crazy stuff. I guess I need a refresher on C#. It seems lines of code are getting executed before they are even encountered in the debugger. Variable hoisting is it?

TLDR: PROBLEM SOLVED

I don't think Visual Studio Ultimate 2015 CTP 6 saves and compiles the literal code you see on the screen. Unless or until you actually "save" the edits to your source files. So all the weirdness is because I am seeing my edited code on screen but that's not what has been compiled into the executing code when I begin the debug session. 


Viewing all articles
Browse latest Browse all 3509

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>