Skip to content

Fix S1144 FP: When having read only Id properties on entity types in EF Core #9416

Open
@mary-georgiou-sonarsource

Description

Description

In EF Core, once properties are being set via the constructor, it can make sense to make some of them read-only. EF Core supports this, but there are some things to look out for:

  • Properties without setters are not mapped by convention. (Doing so tends to map properties that should not be mapped, such as computed properties.)
  • Using automatically generated key values requires a key property that is read-write, since the key value needs to be set by the key generator when inserting new entities.

An easy way to avoid these things is to use private setters.
Although the setters seem unused, in reality, EF Core is using the field in an extralinguistic manner.

See MSDN docs for more information.

Repro steps

public class Blog
{
    public Blog(int id, string name,)
    {
        Name = name;
    }

    public int Id { get; private set; }            // FP
    public string Name { get; private set; }
}

Expected behavior

It should not raise for properties on classes representing entities in EF core that have unused private setters.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Area: C#C# rules related issues.Type: False PositiveRule IS triggered when it shouldn't be.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions