2

I am just getting started with the ADO.NET Entity Data Model and I was curious as to what was the best way of extending my entities behaviour.

Say I have a table called "Customers" and I create an entity model based on this table. I now have a "Customers" entity class. I want to now add some methods to this class and also make it implement an interface.

Should I directly change the code in the Designer.cs file, or should I inherit from Customers and do my extra work from there?

Any suggestions/advice would be great.

Thanks.

James
  • 80,725
  • 18
  • 167
  • 237

1 Answers1

2

Do not change the designer code. Entities creates partial classes so you can do something like this

public partial class Customer: IAmACoolInterface
{
 //implement stuff here



 }
Rob
  • 1,320
  • 1
  • 8
  • 14
  • Yup that's pretty much the way it's done with the entity framework. – Odd Aug 26 '09 at 23:42
  • 1
    This is a correct answer (+1), but I'll add that it's generally a mistake to put business methods on entity types. Entity types are for mapping. Business logic is a separate concern. If you need to execute a method, use LINQ to entities to project onto a POCO type and execute methods there. IMHO! – Craig Stuntz Aug 27 '09 at 13:08
  • Just to add to this. When I created a foreign key in my table, the Entity gives me an Object and a Reference Object of whatever table the foreign key is in, instead of the ID. Why does it do this? – James Aug 28 '09 at 07:50
  • 1
    James, this explains the thinking on FKs and changes in EF 4 in great detaiol: http://blogs.msdn.com/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx – Craig Stuntz Aug 28 '09 at 21:09