brunosp86
brunosp86

Reputation: 676

How to create strongly-typed entities

Reading this blog post about Primitive Obsession I was wondering:

  1. How can I create a strongly-typed entity using Entity Framework Code First? (specially regarding mapping these strong types to primitive types in SqlServer)

  2. Does that make sense? (I've never found any examples/tutorials on the web with this kind of approach)

Suppose we have an Address entity/class with a strongly-typed Zipcode property as we can see here.

Upvotes: 1

Views: 591

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364249

Edited version according to comment:

It make sense but you have never found any example because it is not directly possible with EF code first. Why?

  • Because EF doesn't support entities or complex types without default constructor and it cannot use constructor with parameters.
  • Because EF doesn't provide any type conversions so EF must map directly to primitive properties.
  • Because EF doesn't support properties without setter.

As a workaround you can use private setter and both default constructor (maybe it doesn't have to be public but I didn't try this) and constructor with parameters and correctly map property (at least with EDMX it works).

Upvotes: 3

Related Questions