Profiling Entity Framework With MvcMiniProfiler - asp.net-mvc

I've got an Asp.net Mvc 3 Application which is now using the MvcMiniProfiler. I'm also using the Entity Framework to access my database, and I'd like to enable the profiler to work with the entity model. So far I've created the Context factory below:
internal class ProfiledContextFactory : IContextFactory
{
public ModelContainer GetContext()
{
var conn = ProfiledDbConnection.Get(GetConnection());
return ObjectContextUtils.CreateObjectContext<ModelContainer>(conn);
}
private static EntityConnection GetConnection()
{
return new EntityConnection(ConfigurationManager.ConnectionStrings["ModelContainer"].ConnectionString);
}
}
When I run the above code, which is called by my repository layer when I start a unit of work, it gets stuck in an infite loop when calling CreateDbCommandDefinition in the MvcMiniProfiler.ProfiledDbServices class.
Any clues what I'm doing wrong?

The problem was my GetConnection was returning the EntityConnection, not the SqlConnection within the EntityConnection. I've now modified my code so that it reads:
private static SqlConnection GetConnection()
{
var connStr = ConfigurationManager.ConnectionStrings["ModelContainer"].ConnectionString;
var entityConnStr = new EntityConnectionStringBuilder(connStr);
return new SqlConnection(entityConnStr.ProviderConnectionString);
}
And it works fine.
I discovered this while looking at this question: Using mvc-mini-profiler with EF 4.0 and Ninject

Related

Connect to 2 different MartenDB datastores with ASP.Net Core

When setting up a MartenDB datastore in ASP.Net Core, you normally put code like this in your Startup.cs:
services.AddMarten(o =>
{
o.Connection(configuration.GetConnectionString("MyDatabase"));
o.AutoCreateSchemaObjects = AutoCreate.All;
o.Serializer(new JsonNetSerializer { EnumStorage = EnumStorage.AsString });
});
This allows you to then inject IDocumentSession and IDocumentStore into your various classes for working with that database.
Now what do you do if you have to connect to a second database? I looked at the ISessionFactory but it is not apparent that you can change the connection string from here. Do you need to manually create and register a new DocumentStore?
To answer my own question, I wound up creating a custom DocumentStore and ISessionFactory for each database I wanted to connect to, and then injecting the custom SessionFactory.
Here's the code (only showing one instance of each class for the sake of brevity. Just replace Db1 with Db2 for the second version of each class):
The custom DocumentStore:
public class Db1Store : DocumentStore
{
public Db1Store(StoreOptions options) : base(options)
{
}
}
The custom SessionFactory:
public class Db1SessionFactory : ISessionFactory
{
private readonly Db1Store store;
public Db1SessionFactory(Db1Store store)
{
this.store = store;
}
public IQuerySession QuerySession()
{
return store.QuerySession();
}
public IDocumentSession OpenSession()
{
return store.OpenSession();
}
}
The service registration (this replaces the services.AddMarten call):
services.AddSingleton(p =>
{
var options = new StoreOptions();
options.Connection(configuration.GetConnectionString("DB1"));
options.AutoCreateSchemaObjects = AutoCreate.All;
options.Serializer(new JsonNetSerializer { EnumStorage = EnumStorage.AsString });
return new Db1Store(options);
});
services.AddSingleton<Db1SessionFactory>();
Then you inject the Db1SessionFactory instance into your class, and run a query like this:
var result = await db1SessionFactory.QuerySession().Query<MyAwesomeTable>().ToListAsync();
Downsides:
I would prefer to inject the QuerySession or DocumentSession, but I can't see a way to do that without moving to Autofac or a similar DI Container that supports named instances.
I am not sure what downsides there will be creating these QuerySession/DocumentSessions in this manner. It may be a bad tradeoff.

How to test database actions in asp.net core integration test?

I am trying to integration test my app.
for example, in my AbController I have PostAb(AbDTO abDTO) method, and I want to test that calling this method will add abDTO to db.
now my test setup:
[SetUp]
public void SetUp()
{
_server = new TestServer(new WebHostBuilder()
.UseEnvironment("testing")
.UseStartup<Startup>());
_client = _server.CreateClient();
}
and my test should look like:
[Test]
public async Task PostAbSanity()
{
await _client.PostAsync("/rest/v1/Ab", new Ab{Id=1});
_context.Abs.find(1).should().NotBeNull();
}
but, how can I inject _context into test? in my app I inject it through constructors, but in tests I cant.
thanks!
I'm assuming you are using SqlLite, so you can edit your SetUp method like this:
[SetUp]
public void SetUp()
{
_server = new TestServer(new WebHostBuilder()
.UseEnvironment("testing")
.UseStartup<Startup>());
_client = _server.CreateClient();
var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>();
optionsBuilder.UseSqlite("Filename=./blog.db");
_context = new BloggingContext(optionsBuilder.Options);
}
Please let me know if this is useful
While I will concede that integration testing with an in-memory database is the most thorough and safe way to work, my specific situation would make this aspect extremely difficult and time consuming. I am using a hosted SQL development server that I overwrite after a period of tests. I worked for several days and found the below to be the process that gave me the results I was looking for.
DotNet Core 2.1
Hexagonal (Onion) architecture needing to test the business logic written in my Data
Access Layer
Program.CS file I added:
//for integration testing
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureServices(services => services.AddAutofac())
.UseStartup<Startup>();
My Integration Test File:
using Core.Data.Entities.Model;
using Core.Data.Entities.UTIA;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Profile.Data.Repos;
using Profile.Domain.DomainObjects;
using Super2.Web;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
namespace Profile.Test
{
public class EmployeeProfileIntegrationTest : IClassFixture<WebApplicationFactory<Startup>>
{
private readonly HttpClient _client;
public EmployeeProfileIntegrationTest(WebApplicationFactory<Startup> factory)
{
_client = factory.CreateClient();
}
private DBContext GetContext()
{
var options = new DbContextOptionsBuilder<DBContext>()
.UseSqlServer("Server = 'Connection String from appsettings'")
.Options;
var context = new DBContext(options);
return context;
}
[Fact]
public async Task TestChildProtectionEmployeeGetData()
{
//Arrange
var ApplicationUserId = XXXX;
var repo = new EmployeeProfileRepo(GetContext());
//ACT
var sut = await repo.GetChildProtectionHistory(ApplicationUserId);
//Assert
var okResult = sut.Should().BeOfType<List<DomainObject>>().Subject;
okResult.First().ApplicationUserId.Should().Be(XXXX);
}
}
}
While I am injecting my context into a different layer, I would suspect that it would work the same for a controller. I included the startup snippet as this caused me some issues as the testserver was looking for IWebHostBuilder instead of the default Core2.1 IWebHost.
Either way, this worked for me. Hope you can get some help from this.

How create multiple db context instances by connecting strings?

In C#, MVC code first application I have
public class CarContext : DbContext { }
class in first version of application. And connection string is like
<add name="CarContext" providerName="System.Data.SqlClient" Integrated Security=true;
connectionString="Data Source=Dragon; Initial Catalog=CarDBv1;"/>
When I run application, first version of database is created - CarDBv1.
Then I edit my CarContext class, for example, add new table, change any property etc., also change version of application, change connection string
Initial Catalog=CarDBv1; to Initial Catalog=CarDBv2; and run project. In this case I have 2 database: CarDBv1 and CarDBv2. But, CarContext class is same in applications.
Now, I need to connect both database and their context(CarContext) from any console application and use their tables for converting, reading etc.
I found a similar answer here: https://stackoverflow.com/a/16860878/1534785
But in my applications context name is same.
How can I create 2 instances for every CarContext in applications by their database connection string?
You can use an overloaded constructor to DbContext to allow contexts to point at and arbitrary database which is NOT declared in app.config.
See the constructor with dbConnection.
public class MyDbContext : DbContext, IContextOptions {
//ctors
protected BosBaseDbContext(string connectionName)
: base(connectionName) { }
protected BosBaseDbContext(DbConnection dbConnection, bool contextOwnsConnection)
: base(dbConnection, contextOwnsConnection) { }
}
usage
//datasource could be localhost, DBName the catalog name
new MyDbContext((GetSqlConn4DbName(dataSource,dbName )),true);
public DbConnection GetSqlConn4DbName(string dataSource, string dbName) {
var sqlConnStringBuilder = new SqlConnectionStringBuilder();
sqlConnStringBuilder.DataSource = String.IsNullOrEmpty(dataSource) ? DefaultDataSource : dataSource;
sqlConnStringBuilder.IntegratedSecurity = true;
sqlConnStringBuilder.MultipleActiveResultSets = true;
var sqlConnFact = new SqlConnectionFactory(sqlConnStringBuilder.ConnectionString);
var sqlConn = sqlConnFact.CreateConnection(dbName);
return sqlConn;
}

Breeze Metadata returns null on GetMetadataFromDbContext method - EF 4.4 MVC4 WebAPI OData

I am developing an application using Breezejs, EF 4.4, MVC4, WebAPI and OData. When breeze makes a call to the Metadata ActionMethod the result is null. We use a code first approach and therefore do not have an EDMX file so I think the error comes about when breeze tries to "re-create" the EDMX in some capacity and it can't. See below for source code where try catch produces an exception.
Example of runtime code where execution fails.
// ~/odata/Analysis/Metadata
[HttpGet]
public string Metadata()
{
return _contextProvider.Metadata();
}
I have managed to include my project into the Breezejs repository located on GitHub. The exception occurs on the line with code "EdmxWriter.WriteEdmx(dbContext, xwriter);". I'm not sure what the issue is however the contents of the "WriteEdmx" method are below as well.
Does anyone have any idea what is going on? The only thing that I can think of is that the context that I am using is inherited from a base context which then inherits from DbContext, but other than that I am completely puzzled and at a stand still. Note: I have read that inheritance is not yet supported in breeze, but I'm not sure if that includes the contexts classes and in a separate test case I used a context that inherited from DbContext and I still received the same error.
private static String GetMetadataFromDbContext(Object context) {
var dbContext = (DbContext) context;
XElement xele;
try {
using (var swriter = new StringWriter()) {
using (var xwriter = new XmlTextWriter(swriter)) {
EdmxWriter.WriteEdmx(dbContext, xwriter);
xele = XElement.Parse(swriter.ToString());
}
}
} catch (Exception e) {
if (e is NotSupportedException) {
// DbContext that fails on WriteEdmx is likely a DataBase first DbContext.
return GetMetadataFromObjectContext(dbContext);
} else {
throw;
}
}
var ns = xele.Name.Namespace;
var conceptualEle = xele.Descendants(ns + "ConceptualModels").First();
var schemaEle = conceptualEle.Elements().First(ele => ele.Name.LocalName == "Schema");
var xDoc = XDocument.Load(schemaEle.CreateReader());
var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
// This is needed because the raw edmx has a different namespace than the CLR types that it references.
xDoc = UpdateCSpaceOSpaceMapping(xDoc, objectContext);
return XDocToJson(xDoc);
}
"WriteEdmx"
// Summary:
// Uses Code First with the given context and writes the resulting Entity Data
// Model to the given writer in EDMX form. This method can only be used with
// context instances that use Code First and create the model internally. The
// method cannot be used for contexts created using Database First or Model
// First, for contexts created using a pre-existing System.Data.Objects.ObjectContext,
// or for contexts created using a pre-existing System.Data.Entity.Infrastructure.DbCompiledModel.
//
// Parameters:
// context:
// The context.
//
// writer:
// The writer.
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Edmx")]
public static void WriteEdmx(DbContext context, XmlWriter writer);
UPDATE: A downgrade from EF 4.4 to EF 4.1 seemed to have solved this problem. An upgrade to EF 5.0 or the nightly build might also do the same.
That's the best I can do regarding this obscure issue. #baffled

Configure Mvc Mini Profiler with Linq DataCotext

I'm using linq-to-sql datacontext for an application.
I have the following class
UserDataContext which I instantiate the table with
var db = new UserDataContext();
How do I make Mvc Mini Profiler insert into that?
I tried to extend UserDataContext with the following partial found at another answer, but the code is never hit.
partial class UserDataContext
{
public static UserDataContext Get()
{
var sqlConnection = new HelpSaudeAPDataContext().Connection;
var profiledConnection = new MvcMiniProfiler.Data.ProfiledDbConnection(new SqlConnection("UserConnectionString"), MiniProfiler.Current);
return new HelpSaudeAPDataContext(profiledConnection);
}
}
Unfortunately there is no single point repository with the var db connection where I could simply pass the mvc mini profiler connection with.
var db = UserDataContext(profilerConnection);
but the code is never hit.
Well, hit it:
var db = UserDataContext.Get();

Resources