views:

211

answers:

1

Hi,

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.

+2  A: 

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
Yup that's pretty much the way it's done with the entity framework.
Odd
Thanks for the advice!
James
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
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
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
Thanks for the link Craig.
James

related questions