Search code examples
c#entity-frameworkpartial-classes

Is there a way to extend an entity framework object so that it doesn't map to the database?


I have an entity framework object that someone else created that maps to a database. I want to extend this object to include fields that I don't want to map to the database (or create new tables or fields in the database) I've been told you can mark a field as [NotMapped] and it won't map to the database. I've been looking into partial classes, and I was wondering if there is a way to create partial classes where one would map to the database and the other wouldn't, instead of marking everything individually as [NotMapped].


Solution

  • As far as the compiler is concerned: all it does is stitch together the various different partial classes end-to-end. There isn't much you can do at a file-by-file (etc) level, except perhaps change the using directives at the top of the page, but that doesn't change what the code means.

    So basically: no, there is no way to do what you describe using partial classes. You would need to decorate each member separately, as you are already doing.