Suppress Columns in Entity Framework Code-First
I'm not a big fan of what I call "aggressive code-first," where your table design is derived from your object design. And, to tell you the truth, I'm not sure I have a good reason for my skepticism. With the one client I worked for who used this process, everything worked out fine (by which I mean that the table design we ended up with was exactly the one I would have recommended).
One issue I have with deriving my table design from my object model is that my object model frequently includes properties that I don't want to have appear as columns in my database. The most obvious example of this are read-only properties whose values are calculated internally (typically, from other properties on the object). Entity Framework is smart enough that it will skip most of these properties but, every once in a while, EF will want to add a column to a table based on a property that I'd prefer EF ignored.
To get EF to ignore the property, just decorate the property with the NotMapped attribute. In this code, for example:
Public Class Customer
Public Property Status As String
Public Property Rating As String
Public Property CreditRating As String
the Customer class' Status and Rating properties will generate columns in the Customer table but the CreditRating property will not.
Posted by Peter Vogel on 09/30/2016 at 2:18 PM