10

In EF 4.3 documentation says:

By default, building a database using Code First does not include private, protected, or internal properties. If you manually included these properties in your model, Code First would ignore any data annotations on those members. This is issue now fixed, and Code First processes the data annotations.

My question is how to included a protected property manully with code first, espically using fluent API?

Smartkid
  • 1,772
  • 2
  • 25
  • 34

2 Answers2

7

Look at http://blog.cincura.net/232147-mapping-private-or-protected-properties-with-code-first-efv4-ctp4/ to the comment from Drew Jones. Not entirely clean, but at least something. :)

Or directly http://blog.cincura.net/232731-mapping-private-protected-properties-in-entity-framework-4-x-code-first/.

cincura.net
  • 4,130
  • 16
  • 40
  • As my domain class and DbContext are seperated in two assemblies, I changed the persistent property to protected internal and add [assembly: InternalsVisibleTo("TheAssemblyWhichContainsDbContext")], now it works. Thanks. – Smartkid Feb 16 '12 at 14:13
  • 1
    I like the idea shared by Drew. It is actually the best workaround I have seen so far because it is still strongly typed and it doesn't couple your mapping with entity - your entity just have some strange nested class which can be made internal and it will be visible only between entity and mapping assembly. Thanks for sharing this. – Ladislav Mrnka Feb 16 '12 at 14:55
  • @LadislavMrnka I [blogged](http://blog.cincura.net/232731-mapping-private-protected-properties-in-entity-framework-4-x-code-first/) about it directly, so it's easily searchable. – cincura.net Feb 17 '12 at 13:09
3

It is not possible with fluent API because fluent API uses strongly typed approach and because of that all accessibility rules are still in place. Documentation says it should be possible with data annotations but I wasn't able to make it work.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670