1

I am investigating some OR/M's and have been unable to find an answer to this question.

I am currently looking at Microsoft's ADO .net entity framework.

Can I override (or use partial classes) to insert custom code into the entities that are created from the database?

It appears that the entities are generated using Xml (not my favourite method of implementation), so I'm not sure if I can put custom code into the classes.

If it can't, can this be done using Linq to SQL?

I have seen T4, and I think it is promising, but at this stage the maintenance moves from entity classes to templates.

1
  • Look into EF 2.0 (.net 4.0). It supports POCO out of the box. Still has XML mapping though yuch. Should be out soon. Commented Mar 1, 2010 at 23:33

1 Answer 1

2

Yes, you can create additional code to existing classes. EF classes are partial. I use it to add validation logic and implement common interfaces. If you want to use DataAnnotations, you have to use additional metadata classes.

To extend your class, just create new class:

public partial class YourEFClassName
{
     //Here you can pute code
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks LukLed, so all I have to do is create a class (say Product) that is also partial with my custom code?
@Russell: Yes. You just add class with the same name and keyword partial.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.