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

Add the option to select the tables/schemas used by --create-dbcontext #3516

Open
chrisevans9629 opened this issue Apr 10, 2024 · 0 comments
Labels

Comments

@chrisevans9629
Copy link

Is your feature request related to a problem? Please describe.
I have a large database where some tables don't work with the scaffolding. Namely, I get the error message:
System.InvalidOperationException: The key {'Id'} cannot be added to keyless type 'Table (Dictionary<string, object>)'.

Describe the solution you'd like
I would like an option to enter the tables and schemas like so:
#!connect mssql --create-dbcontext --tables User,Order --schemas dbo --kernel-name name connection

Describe alternatives you've considered
The only workaround I see is not using this feature and building the DbContext from scratch, or taking the following code and copying/pasting the output into the notebook, which is very ugly:

var services = new ServiceCollection();
services.AddEntityFrameworkDesignTimeServices();
var providerAssembly = Assembly.Load("Microsoft.EntityFrameworkCore.SqlServer");
var providerServicesAttribute = providerAssembly.GetCustomAttribute<DesignTimeProviderServicesAttribute>();
var providerServicesType = providerAssembly.GetType(providerServicesAttribute.TypeName);
var providerServices = (IDesignTimeServices)Activator.CreateInstance(providerServicesType);
providerServices.ConfigureDesignTimeServices(services);

var serviceProvider = services.BuildServiceProvider();
var scaffolder = serviceProvider.GetService<IReverseEngineerScaffolder>();

var model = scaffolder.ScaffoldModel(
    connection,
    new DatabaseModelFactoryOptions(new []{"dbo.Order", "dbo.User"}, new string[0]),
    new ModelReverseEngineerOptions(),
    new ModelCodeGenerationOptions()
    {
        ContextName = "TestContext",
        ModelNamespace = "TestFiles",
           
    });


model.Display();

var code = @"using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;";

foreach (var file in  new[] { model.ContextFile.Code }.Concat(model.AdditionalFiles.Select(f => f.Code)))
{
    var namespaceToFind = "namespace TestFiles;";
    var headerSize = file.LastIndexOf(namespaceToFind)  + namespaceToFind.Length;
    var fileCode = file
        // remove namespaces, which don't compile in Roslyn scripting
        .Substring(headerSize).Trim();

    code += fileCode;
}

code
@chrisevans9629 chrisevans9629 added the enhancement New feature or request label Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants