To define an Entity Framework -serializable classes (EF Code First) we need to use public virtual properties as data holders, that later on will be used as a columns in the DB. This rids a developer off an ability to initialize an instance of an entity from a constructor. I want my entities to not only be a data “buckets”, but to also have behavior, such as constructor instantiation for new objects, and methods for data manipulation within the object.
Methods I can have I suppose, but what should I do to overcome the constructor limitation? Should I introduce the wrapper class that will instantiate an entity property-by-property and use it instead?
You can have methods “as you want”, as well as properties without setters that won’t be mapped in the db. Or properties with a
NotMappedAttribute, which… won’t be mapped.Note : only the navigation properties (references and collections) need to be virtual for lazy loading. Not the primitive. I think this is an NHnibernate requirement.
For the constructor(s), you can add as many parametrized constructors as you want, you just need to have ALSO a parameterless one.
By the way, you can use object initializers as well.