Map only specific properties from a model to table fields. Entity Framework

c# entity-framework entity-framework-6

Question

I'm running out of ideas how to implement the thing in a simple way.

Assuming we have class which also serves as a model of a database table, but the case is that I require to map only the few properties to be fields in the table.

The annotation (using [NotMapped] annotation on a model class properties) approach is not a good one for me, we are following the Fluent API way...

I know I can Ignore(p => p.SomeProperty) but it is still a wrong solution because I have a lot of properties to ignore.

I've been thinking of some kind of templates using abstract classes or interfaces containing the properties I require for each class->table relation, and processing them by reflection passing the template and retrieving EntityTypeConfiguration but it seems for me a very messy solution.

Thank you, and May the force be with you :)

1
0
12/23/2015 9:44:17 AM

Popular Answer

You said:

I know I can Ignore(p => p.SomeProperty) but it is still a wrong solution because I have a lot of properties to ignore.

Maybe first of all ask yourself why do you have so many properties, maybe your entity should be spitted into several entities? It seems more like you have to solve an architectural issue rather than a technical one.

I'd consider one of the following approaches:

  • You could have several entities mapped to the same table, but each entity containing just properties that are required in a specific use case. (focus on Single responsibility principle)
  • I don't know the exact structure of your DB model, but it could be the case that you can use Table per Hierarchy pattern.
  • If none of above is still a solution, consider at least grouping these properties into complex types.
1
12/24/2015 11:02:19 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow