Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
voidlps opened this issue Mar 14, 2024 · 0 comments

Comments

@voidlps
Copy link

voidlps commented Mar 14, 2024

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant