John Aschenbrenner
John Aschenbrenner

Reputation: 41

entity framework code first private set for identity column

It seems to me that almost by default all code first POCO's should have private setters for their primary key a.k.a. auto-generated Id.

Like this - public int id { get; private set; }

Is this an incorrect assumption? I do not want my API to allow setting of an auto-generated column.

Upvotes: 0

Views: 378

Answers (1)

Eyal Perry
Eyal Perry

Reputation: 2285

Exposing a public setter should not be an issue since it is unadvised to even expose this POCO outside the Data Access Object layer..

Exposing a POCO decorated with a specific framework's attributes, or even a POCO which discloses some kind of information regarding storage (Entity Relational Database, in this instance) is a bad practice.

Consider wrapping it in an interface and returning it as an instance of that interface. This way you get to enjoy the best of both worlds. Exposing the properties which are necessary and allowing to set only a part of them.

In any case, I do not think that EF will like the private setter thing too much.

Upvotes: 1

Related Questions