Skip to content

When call AddDataContextSQL with sqlOptions will lead to fail to connect to DB #40

@voidlps

Description

@voidlps

Describe the bug
AddDataContextSQL provides optional param to extend SQL dbContext like using NetToplogySutie
However it leads to fail to connect to db using correct connection string

To Reproduce
Steps to reproduce the behavior:

  1. Init ApiGenerator like this:
builder.Services.AddApiGeneratorServices()
        .AddAssemblyWithOData(Assembly.GetExecutingAssembly())
        .AddDataContextSQL( x=> x.UseNetTopologySuite())
        .AddOData()
        .AddSwagger(true);
  1. Open swagger to test GET API
  2. HTTP 500 ERROR will be returned
 ---> System.InvalidOperationException: A relational store has been configured without specifying either the DbConnection or connection string to use.
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnection()

Expected behavior
200 OK and data should be returned correctly

Desktop (please complete the following information):

  • OS: Windows 11
  • Browser Edge
  • Version 122.0.2365.80 / 64bit

Additional context

  1. Workaround: Register GenericDbContext using AddDbContext and UseSqlServer directly before call AddDataContextSQL
// Add API Generator and load data
var api_generator = builder.Services.AddApiGeneratorServices()
                .AddAssemblyWithOData(Assembly.GetExecutingAssembly());
api_generator.Services.AddDbContext<GenericDbContext>(options => options.UseSqlServer(
        myconnectionstring,
        x => x.UseNetTopologySuite()).EnableSensitiveDataLogging()
           .LogTo(Console.WriteLine, LogLevel.Information));

api_generator.AddDataContextSQL()
.AddOData()
       .AddSwagger(true);
  1. Should be due to here and here
    connection string is not passed to UseSqlServer

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions