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

Code generated by dotnet ef dbcontext scaffold should be marked as auto-generated #28180

Closed
0xced opened this issue Jun 7, 2022 · 3 comments
Closed

Comments

@0xced
Copy link
Contributor

0xced commented Jun 7, 2022

Running dotnet ef dbcontext scaffold generates code which often produces analyzer warnings, e.g. using directive that can be removed, virtual member call in constructor, auto-property accessor never used, etc.

Adding an <auto-generated/> comment in the generated code would instruct the analyzers to ignore those warnings. See https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/configuration-options#exclude-generated-code and dotnet/roslyn#3705

0xced added a commit to 0xced/efcore that referenced this issue Jun 7, 2022
Adding the `<auto-generated/>` tag prevents analyzers to generate spurious warnings.

Fixes dotnet#28180
@smitpatel
Copy link
Member

Duplicate of #23514

@ajcvickers
Copy link
Member

@0xced Note that the T4 templates work in 7.0 (#4038) will make it easy to add this if you need it.

@0xced
Copy link
Contributor Author

0xced commented Jun 8, 2022

Thanks Arthur, that's good to know. I think it would be better if <auto-generated/> is there by default, without having to manually tweak templates.

I'm a bit disappointed that #28181 was not merged, it looked like a really quick win to me, literally a 6 lines change (ignoring the tests) that would benefit everyone for a good experience out of the box.

I just came across this code I wrote some time ago to workaround this issue. 😕

private static void AddAutogeneratedComment(DirectoryInfo directory)
{
    Parallel.ForEach(directory.GetFiles("*.cs"), file =>
    {
        var lines = new[]
        {
            "// <auto-generated />",
            "#nullable enable",
            "",
        }.Concat(File.ReadAllLines(file.FullName));
        File.WriteAllLines(file.FullName, lines);
    });
}

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants