DeeJayTC / net-dynamic-api

A library that turns your model into a fully working API, define your model as markdown (soon), json or c#.
https://www.tcdev.de/tcdev-api-generator-getting-started
MIT License
193 stars 18 forks source link

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

Open voidlps opened 8 months ago

voidlps commented 8 months ago

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);
  2. Open swagger to test GET API
  3. 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):

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);



2. Should be due to [here](https://github.com/DeeJayTC/net-dynamic-api/blob/main/src/TCDev.APIGenerator.Data.SQL/ServiceExtension.cs#L80) and [here](https://github.com/DeeJayTC/net-dynamic-api/blob/main/src/TCDev.APIGenerator.Data.SQL/ServiceExtension.cs#L102)
connection string is not passed to UseSqlServer
DeeJayTC commented 3 months ago

Hey @voidlps Didn't have time to check all this in quite a while, thanks for the submission and happy you found a workaround.

If you want to provide that workaround as a PR i'm happy to add it!

voidlps commented 2 months ago

@DeeJayTC Sure, I think I will try to setup a dev & test env for this project and then submit a PR for the workaround or fix.