Scrum Masters

Entity Framework Core and Private Properties


& #13;. .

Item oriented style is everything about encapsulation. A class supplies techniques to control its information and just enables control that keeps the items’ state right. That works fine in theory, however in reality items require to be loaded/materialized from a database and this typically indicates that residential or commercial property setters need to be public. However not with Entity Structure Core! Entity Structure Core utilizes some reflection magic to access the behind the scenes personal properties. So it is now possible to compose “genuine” things oriented entities and continue them to the database.

Including a typical entity class, with no factors to consider required to perseverance works completely well with EF Core.

 public  class MyEntity.
 {
   public MyEntity( string name)
   {.
Call  = name;
  } 
.
 public  int Id  {  get;  personal  set; } 
   public  string Name  {  get;  personal  set; } 
.
 public  space Rename( string newName)
   {.
Call  = newName;
  } 
} 

There are some limitations that are in impact though. So it is advised to make some little changes to the entities.

The very first constraint is that when utilizing a builder that takes specifications it need to be specifications that EF can bind to a residential or commercial property. Altering the name of the name criterion to something else breaks things:

 Unhandled Exception: System.InvalidOperationException: No appropriate builder discovered for.
entity type 'MyEntity'. The following specifications might not be bound to residential or commercial properties of.
the entity: 'foo'.

Although the names match, there is a disadvantage with having entity structure call a builder with service reasoning when filling information from the database. It is much better to include a parameterless builder that EF can utilize to develop items behind the scenes. It can even be personal.

To sum up EF Core is Completely REMARKABLE. Thanks to this function it revives the power of things orientation and encapsulation.

A total working code example is offered in the EfCorePrivate repo on my GitHub Account.

. Published in . C# on 2018-06-06|Tagged Entity Structure Core
. . . .

Source link .