0

Please pardon the long-winded title to this post. Basically, I have an entity model that was generated by Entity Framework 5 using the Database First method. Now I would like to develop those POCO entity classes by adding validation logic and anything else I might need to go in there. The problem... (and this is so glaring I can't believe I couldn't find any answer to this on the web)... is that any time my database schema should change and I use the "Update Model from Database" command from my model diagram, the POCO classes are re-generated and all custom code is lost.

As far as I understand, the "domain model" layer in an MVVM application is largely comprised of what EF generates (in database first approach), plus validation (perhaps implementing the IDataErrorInfo. But if this was the right way to do it, why would this be wiped away when using database-first model updates?

What am I missing?

Thanks

BCA
  • 7,776
  • 3
  • 38
  • 53

1 Answers1

1

You should use partial classes. That is why they are there.

If you want to know the basics see this short example: http://www.dotnetperls.com/partial

Here is more on partial classes from the msdn: http://msdn.microsoft.com/en-us/library/vstudio/wa80x488.aspx

If you want to use your own namespaces in the genarted code, you can refer to this SO question: Entity Framework 5 partial classes not working

Community
  • 1
  • 1
Ondrej Peterka
  • 3,349
  • 4
  • 35
  • 49
  • thanks for the links. Yes, partial classes seems like a start in the right direction: using that approach I can implement IDataErrorInfo. But what about adding attributes to the generated properties, or adding custom logic to those properties? If one could implement partial properties (just like partial methods), then I'd be set. Any ideas? – BCA Jun 03 '13 at 14:47
  • Check this article: http://weblogs.asp.net/scottgu/archive/2007/07/11/linq-to-sql-part-4-updating-our-database.aspx There are several hooks (onChange, insert, update etc.) which you can employ and implement them in the partial class. – Ondrej Peterka Jun 03 '13 at 15:19
  • I believe that article applies to the old EF 4 that did not involve POCO classes – BCA Jun 04 '13 at 17:10
  • If you want to add attributes to generated classes you can use MetadataType. Here is a nice article about this problem: http://ardalis.com/adding-attributes-to-generated-classes – Carbosound1 Feb 03 '14 at 13:35