Extending Entity Framework generated classes

I had an epiphany today, it may have been obvious to a lot of people but it was one of those weird moments where I realised something which was right under my nose the whole time… So this may seem really stupid

I was reading through Martin Fowlers refactoring book and was looking at the code smell "data classes" and was thinking about how every company I've worked that uses EF and a lot of popular books on MVC/tuorials I've read heavily use "data classes" generated by EF, then I suddenly remember the partial keyword that comes along with these classes which could be used to add functions to these classes while allowing them to still be generated from your DB.

I may have seen this used before for some simple validations, but at least not for completely encapsulating a classes entire functionality

I'm kind of confused how I've used this so long yet never seen anyone use this partial keyword to give functionality to your entities, does it work well? Is it good practice to do this?

by presidentchimp via /r/csharp

Leave a Reply