Skip to content

Enable nullability in migrations and model snapshots #18950

Description

@TorreyGarland

I am generating migrations files for EF core with the new nullable reference types analysis turned on for an ASP.NET core app (3.0).

The migration file generated has several "new object[]" declarations that VS is flagging for CS8625 warnings (screenshot included).

image

Is there a work-around or change coming so that we don't have to do a global suppression. I want the nullable reference analysis, but just not for these migration files.

Activity

  1. added this to the Backlog milestone on Nov 18, 2019
  2. ajcvickers commented on Nov 18, 2019

    @ajcvickers
    Contributor

    @TorreyGarland Putting this in the backlog to consider in the future. The global suppression seems like the best workaround for now.

  3. Timovzl commented on Nov 30, 2020

    @Timovzl

    @ajcvickers @TorreyGarland Could either of you provide an example of such a global supression? I'm trying to get them to work, but with no success.

    [assembly: SuppressMessage("Code Quality", "CS8625:Cannot convert null literal to non-nullable reference type.",
    Justification = "EF migrations.",
    Scope = "NamespaceAndDescendants", Target = "~N:My.Namespace.Migrations")]
    

    I did read somewhere that the NamespaceAndDescendants scope was broken in VS 16.7, and that it would be fixed in 16.8. I'm now on 16.8.2, but the suppression is not helping.

  4. roji commented on Nov 30, 2020

    @roji
    Member

    I think that by global suppression, it was meant that you don't activate the NRT feature at the project level (i.e. <Nullable>disabled</Nullable>). You can place your migrations in a separate project to keep nullability enabled in your actual code, if you want.

    Another option is to place a #nullable disable at the top of each migration file.

  5. removed this from the Backlog milestone on Nov 30, 2020
  6. roji commented on Nov 30, 2020

    @roji
    Member

    Clearing milestone to discuss if we want to do something for 6.0.

  7. Timovzl commented on Nov 30, 2020

    @Timovzl

    @roji I'm pretty sure global suppressions is generally the term for what I mentioned above, so I assume that's what @TorreyGarland meant. I can't see why it's not working, though.

    Disabling nullable for the entire project is too impactful, unfortunately. Adding #nullable disable at the top of each migration file would work, as would disabling the warnings from those locations.

    I'm still hoping for a solution that makes the problem go away, instead of requiring each developer to remember for each new migration. We have "treat warnings as errors" enabled in the DevOps pipeline, to prevent the solution from becoming riddled with warnings... so it's quite a nuisance. 😛

  8. roji commented on Nov 30, 2020

    @roji
    Member

    You're right - global suppressions should indeed also work, I'm not sure why the above doesn't. I also agree we should provide a better solution than requiring developers to handle this themselves every time.

  9. added this to the 6.0.0 milestone on Dec 1, 2020
  10. removed this from the 6.0.0 milestone on May 6, 2021
  11. 16 remaining items

  12. roji commented on Jan 24, 2022

    @roji
    Member

    Scenarios:

  13. modified the milestones: 7.0.0, Backlog on Jul 7, 2022
  14. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions