Skip to content
Discussion options

You must be logged in to vote

I think there are several ways to use DI to manage database connections. The key thing to understand is that each server-side data portal request runs in its own DI scope, so all scoped services are created for the lifetime of the request and are disposed/closed at the end of the request.

You can't subclass SqlConnection, so it needs to be wrapped in another class. For example, you could have a base class like this:

using Microsoft.Data.SqlClient;

namespace CslaDataAccess
{
  /// <summary>
  /// Base class for scoped db connection provider
  /// </summary>
  public class DbProvider
  {
    public virtual SqlConnection? DbConnection { get; protected set; }
    public virtual SqlTransaction? 

Replies: 2 comments 9 replies

Comment options

You must be logged in to vote
7 replies
@rockfordlhotka
Comment options

@mtavares628
Comment options

mtavares628 Jun 10, 2022
Collaborator Author

@rockfordlhotka
Comment options

@TheCakeMonster
Comment options

@TheCakeMonster
Comment options

Comment options

You must be logged in to vote
2 replies
@Chicagoan2016
Comment options

@mtavares628
Comment options

mtavares628 Jul 6, 2022
Collaborator Author

Answer selected by mtavares628
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants