Glimpse EF6 SQL Tab disabled / not Capturing Data Access - c#

I have used Glimpse before and really liked it so when this new project came up I went right to it. This time however I am using the EF plugin which is new to me. Further the project is broken up into different layers. Overall glimpse is working and it is just the SQL tab that is not active. Logging is turned on with no errors.
My Solution is like so:
VS 2015
Repository Project EF 6.0.0.0
Glimpse ADO 1.7.3.0
Glimpse Core 1.8.6.0
MVC & WebAPI Project MVC 5.2.3 WebApi
Glimpse ADO 1.7.3.0
Glimpse ASP.Net
Glimpse Core 1.8.6.0
Glimpse EF6 1.6.5
Glimpse MVC 5
So I am creating a manual SQL connection and most likely that is where my mistake is?
In the repository I establish my context like this:
public class PROJDataContext : DbContext, IPROJDataContext
{
public PROJDataContext() : base(PROJConnection, true)
{ }
private static GlimpseDbConnection PROJConnection
{
get
{
//glimpse wrapper
var conxSection = ConfigurationManager.ConnectionStrings["PROJData"];
return new GlimpseDbConnection(new SqlConnection(conxSection.ConnectionString));
}
}
When I fire up the project localhost the SQL tab is disabled. I am assuming this is because the db has not been accessed yet.
I browse to a page that passes request to WebApi. WebApi queries the repository. Data is retrieved from repository and sent back to WebApi which then passes it back to the MVC controller action which then returns a view showing the data.
Glimpse accurately tracks ALL of this except the db portion so any pointers would be greatly appreciated.
Update...when I wrote this up it was EOD Friday. Talking this through with a colleague today I think I realized the problem. My MVC app which has the Glimpse hooks does NOT call SQL instead it calls off to web api and web api calls the repository which has the Glimpse SQL hooks.

Related

Associate data to default ASP.NET UAM user using Entity Framework Core

Trying to create a simple MVC application that has basic user account management with social account sign-up.
Created ASP.NET Core 2.2 app with individual authentication, added EF core via NuGet (sqlserver and tools). I have built some basic ASP.NET MVC apps and MVC apps in other languages previously.
I got Facebook login to work per this tutorial.
Now I'm lost.
There's a "Data" directory with a bunch of code in it already, and the normal MVC directories. However, I don't see the MVC files that would correlate to any of the pages or actions associated with the account management I can clearly access when I launch the application, including /Identity/Account/Login, /Identity/Account/Manage, /Identity/Account/Manage/SetPassword, etc.
While having all of this functionality up and running in minutes is cool, already I'm at a disadvantage of not knowing how or why it works. Searching for anything I would expect to shed some light on this gives me technical documentation that makes my head spin and provides zero enlightenment.
I want to have a collection of data sets ("Books") associated to each user. One User to many Books. Given that the User (account?) model is not in the Models directory of the MVC application I don't think that's where it's supposed to be added. And if it is, I don't know how to create that association with the User.
This is what I'm seeing in a file called ApplicationDbContextModelSnapshot.cs
namespace Bookshelf.Data.Migrations
[DbContext(typeof(ApplicationDbContext))]
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
...
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken();
b.Property<string>("Name")
.HasMaxLength(256);
b.Property<string>("NormalizedName")
.HasMaxLength(256);
b.HasKey("Id");
b.HasIndex("NormalizedName")
.IsUnique()
.HasName("RoleNameIndex")
.HasFilter("[NormalizedName] IS NOT NULL");
b.ToTable("AspNetRoles");
});
... and so on, which looks fairly readable, but does not match up to any of the tutorials I've seen so far.
TL;DR:
Where/how do I correctly create a new models? Is it like this in the Models directory (how I'm used to) or is this obsolete?
Either way, how do I correctly associate data with a user given the baked-in user account management for ASP.NET Core 2.2?
There's a "Data" directory with a bunch of code in it already, and the normal MVC directories. However, I don't see the MVC files that would correlate to any of the pages or actions associated with the account management I can clearly access when I launch the application, including /Identity/Account/Login, /Identity/Account/Manage, /Identity/Account/Manage/SetPassword, etc.
This is because from ASP.NET Core 2.1 Identity is being provided as Razor Class Library with the ASP.NET Core project templates. If you want to see those Identity related codes and customize then you have to Scaffold Identity in your project.
Here is more details about this: Scaffold Identity in ASP.NET Core projects
Moreover if you need ASP.NET Core Identity in MVC format then here is my GitHub Repository where Razor Page Identity has been converted to MVC in ASP.NET Core 2.2.

ASP.NET Web API with Linq2Couchbase dependancy injection

I'm new to ASP.NET Web API projects and also new to Couchbase. I'm trying to follow the instructions here: https://github.com/couchbaselabs/Linq2Couchbase/blob/master/docs/bucket-context.md
Using a blank project and the code provided, I get the error: "No parameterless constructor defined for this object". I know that I need to "inject" the BucketContext in some way, but I don't know where to put that, any ideas?
Perhaps following this tutorial will help you understand the Couchbase SDK in a bit more detail and let you understand the initialisation "challenge" you have.
http://blog.couchbase.com/2015/november/couchbase-dotnet-client-sdk-tutorial
In short, Couchbase Cluster is a "heavy" object and it's recommended to keep the object for the lifetime of the app. In WEB API that means that init should be done on app start. Depending on what version of ASP.NET you are using (ASP.NET 4.5 or ASP.NET vNEXT) init is done/recommended to be done different places.
ASP.NET 4.5 = global.asax
vNEXT = APP_START folder (look for other initialisations)
The above project/tutorial will explain step by step how to do the init.
When init is in place, linq2couchbase should work :)
Please let me know if this helped.

Using log4net to log in Api from class library

I have a webapi for which I am trying to debug.
I use a generic repository structure so all my entity framework calls are made in a separate class library.
So my web Api post end point effectively just calls service.insert(entity). Where the generic insert is in the separate class library.
Logging is currently setup and working in the api. Now I want to log the insert Sql generated by entity framework in the parent applications text log file (as something strange is going on in the live environment)
How would I go about doing this please?
How to do this depends on what version of Entity Frameework you are using. In EF6 and later it is simple:
using (var context = new DataContext())
{
// log is a log4net logger
context.Database.Log = message => log.Debug(message);
// insert the entity
}
See this blog series for more information - part 3 shows an example of logging to NLog with a command interceptor - and this page for options relating to earlier versions of EF.

Does Identity Owin require LazyLoading?

tl;dr: Identity seems to require that LazyLoading NOT be disabled; is this true, and what is the cleanest workaround?
I did some basic A-B testing on a simple C# ASP.NET 4.5.1 MVC-5 web application using EntityFramework 6.0.2, Identity EntityFramework 1.0.0, and Identity Owin 1.0.0 and it appears that Owin requires that lazy loading is NOT disabled in the ApplicationContext constructor.
To replicate the issue just make a quick MVC app using Visual Studio 2013, use the MVC template, leave everything at the defaults except uncomment the line: 'app.UseGoogleAuthentication();' in App_Start/Startup.Auth.cs. Run the app and use Google to login, complete the abbreviated registration page it takes you to and go to account/manage. You should see 2 buttons for Google at the bottom. Stop the app.
Now go to ApplicationContext.cs and change the constructor as shown in this code snippet:
public ApplicationContext() : base("DefaultConnection") { } //Works!
public ApplicationContext() : base("DefaultConnection")
{
this.Configuration.LazyLoadingEnabled = false;
} //Does not work
Retry the test. Only 1 Google button should be visible. With LazyLoadingEnabled = false the User Roles, Logins (poss Claims also) aren't loaded.
My theory is that this is a Microsoft oversight/'future feature' as Identity EntityFramework and Identity Owin are both version 1.0.0.
My question is, can this test be confirmed, and what is the cleanest work around?
For my purposes I will just use .ToList() and other methods for forcing EagerLoading when I want to use it. I don't truly need LazyLoading disabled, it's just a safer way to code if you want to always use eager loading. i.e. you miss one spot, it makes it to production, and you have a nice bug where in some View you are iterating through a Model and for Model.x.y y == null and the database connection has been disposed.
Let's not get into Identity vs. other (more robust) methods, or:
using (DatabaseContext) { //Database query }
or calling dispose on every method vs letting the connection be disposed automatically. This is a scenario where you have to use Identity Owin, and dispose of all database calls ASAP. There ought to be something I'm missing, or maybe Identity really is just that incomplete right now.
Yes this was a bug we have fixed in the 2.0.0-alpha1 release. With lazyLoading explicitly disabled prior, EF would not load the associated user entities automatically (logins/claims/roles)

How do i do caching the right way in a ASP.NET MVC 3.0 project?

I have a ASP.NET MVC 3.0 project with a MySQL database that going to need caching on to get faster to load for users.
What is your best tip on how to do caching on a ASP.NET MVC project?
If you want server-side caching (e.g caching "data"), you should look into .NET 4.0's new ObjectCache.
If you want output cache, you should decorate your action methods with said attribute, much like with Web Forms.
E.g:
[HttpGet]
[OutputCache(Duration = 60*5, VaryByParam("*")] // cache for 5 mins
public ActionResult GetSomethingThatDoesntChangeOften(int someParam, string someOtherParam)
{
// some code ...
}
You should use one or both, depending on the situation at hand.
E.g "weighty" database calls should be cached on the web server (e.g "data caching").
And HTML that doesn't change often should be cached on the client with output cache.
We use the Caching Application Block from Microsoft
http://msdn.microsoft.com/en-us/library/ff664753(v=pandp.50).aspx

Categories

Resources