question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scaffolding Issue with .NET6.0 and Customized Interceptor

See original GitHub issue

Describe your issue

Dear LINQ TO DB Dev Team,

I have issue when using linq2db CLI scaffolding with .NET6.0 with customized interceptor entity.

I created a new class which is a derivative of ScaffoldInterceptors and inserted my source code (see section below ‘Interceptor Class’). In cli.json file, in the property [general], I added the key [customize] with value of built DLL.

I use net6.0, linq2db 4.3.0 and linq2db.Tools 4.3.0 and when scaffolding, it shows an error. In your documentation you wrote that customize option, can be used on .NET 3.1 or later versions and that it should be compatible with current runtime. After I changed project’s target framework on netcoreapp3.1 and JSON customize option’s value to new built in DLL “.\bin\Debug\netcoreapp3.1\PROJ.dll”, everything works smooth and without errors.

Summary:

When I use the framework .NET6.0 and scaffolding with customized interceptor entity. It fails. When I use the framework .NET3.1 and scaffolding with customized interceptor entity. Everything runs perfectly fine.

Am I doing something wrong? What is the problem with customized scaffolding in the .NET6.0 environment?

Steps to reproduce

dotnet linq2db scaffold --import "cli.json"

cli.json

"general": {
  "output": ".\\Models",
  "overwrite": true,
  "provider": "SQLServer",
  "connection": "<< YOUR DB CONN STRING HERE >>",
  "template": "default",
  "customize": ".\\bin\\Debug\\net6.0\\PROJ.dll"
}

Interceptor Class

public class Intreceptor : ScaffoldInterceptors
{
    // several use-cases cases of entity customization
    public override void PreprocessEntity(ITypeParser typeParser, EntityModel entityModel)
    {
        // for table with name "alltypes" we cannot recognize separate words to properly generate class name
        // let's modify generated entity class name in model
        if (entityModel.Class.Name.StartsWith("AspNet"))
            entityModel.Class.Name = entityModel.Class.Name.Replace("AspNet", String.Empty);

        // mark column as non-editable
        var creatorColumn = entityModel.Columns.Where(c => c.Metadata.Name == "created_by").FirstOrDefault();
        if (creatorColumn != null)
            creatorColumn.Metadata.SkipOnUpdate = true;
    }
  }

Error

Unhandled exception: Unable to load one or more of the requested types.  
Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
...
at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.RuntimeModule.GetTypes()
   at System.Reflection.Assembly.GetTypes()
   at LinqToDB.CommandLine.ScaffoldCommand.LoadInterceptorsFromAssembly(String sourcePath, Assembly assembly, ScaffoldOptions options)
   at LinqToDB.CommandLine.ScaffoldCommand.LoadInterceptorsFromAssembly(String assemblyPath, ScaffoldOptions options)
   at LinqToDB.CommandLine.ScaffoldCommand.LoadInterceptors(String interceptorsPath, ScaffoldOptions options)
   at LinqToDB.CommandLine.ScaffoldCommand.Execute(CliController controller, String[] rawArgs, Dictionary`2 options, IReadOnlyCollection`1 unknownArgs)
   at LinqToDB.CommandLine.CliController.Execute(String[] args)
   at LinqToDB.Tools.Program.Main(String[] args)

Environment details

Packages: Linq To DB version: 4.3.0 Linq To DB Tools version: 4.3.0

Database (with version): SQL Server 2019

.NET Version: 6.0

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:13 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
viceroypenguincommented, Nov 4, 2022

I use File Scoped Namespaces and Global Usings with C# 10 and targeting .netstandard 2.0, .net 3.1, .net 5.0, and .net 6.0 just fine. Mainly, what you need to have is:

  <PropertyGroup>
    <LangVersion>10</LangVersion>
    <TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
  </PropertyGroup>

The <LangVersion/> tag will allow you to specify which version of C#, whether latest or 10. The <TargetFrameworks/> tag will let you specify which TFMs you want to reference. Once you’ve done this, most C#10 features will be available natively, for both 3.1 and 6.0. This includes file-scoped namespaces, global usings, etc. If you want to use implicit usings, you can add <ImplicitUsings>enable</ImplicitUsings> as well.

However, as stated, some features require attributes in order to build correctly; you can find a full list on the PolySharp GH page of which features these are and the attributes necessary. The easiest way to address this is to use that library to polyfill the necessary attributes - then you can use all C#10 features for any .netstandard, .netcore, or .net TFM.

0reactions
crobancommented, Nov 4, 2022

@viceroypenguin you are the KING 🥇 I will try that for sure!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error trying to scaffold a view in ASP.NET Core 6.0 MVC
I'm trying to scaffold a new razor view using Visual Studio. I select a template, my model and my DbContext , then I...
Read more >
NullReferenceException scaffolding when no primary key set
Workaround. You can work around this by using custom templates and adding a null check to DbContext.t4 line 219: <#= foreignKey ...
Read more >
7.2.2 Scaffolding an Existing Database in EF Core
Scaffolding a database produces an Entity Framework model from an existing database. The resulting entities are created and mapped to the tables in...
Read more >
What's New in EF Core 6.0
EF Core 6.0 contains several improvements when reverse engineering an EF model from an existing database. Scaffolding many-to-many relationships.
Read more >
LINQ to DB CLI tools
NET type for database type, not known to scaffold utility (e.g. some custom database type); Data model interceptors. Those interceptors work with generate ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found