Search code examples
c#entity-frameworkef-database-first

Entity framework DB first - private setter


For every table, EF generates a partial class, where all fields are publicly accessible, for example

public int ID { get; set; }

Is it possible to make the set private? I would then only allow the field to be changed calling my custom public method.


Solution

  • Using designer, you can change visibility of setter from property grid. Just click on property, press F4, and select desired visibility:

    enter image description here

    But I recommend you to select protected instead of private, since there could be some difficulties (e.g. see this question).