Scaffolding Issue with .NET6.0 and Customized Interceptor
See original GitHub issueDescribe 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:
- Created a year ago
- Reactions:2
- Comments:13 (11 by maintainers)
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:
The
<LangVersion/>
tag will allow you to specify which version of C#, whetherlatest
or10
. 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.
@viceroypenguin you are the KING 🥇 I will try that for sure!