Tuesday, October 11, 2011

Extenend ORM generated class

In this post I am going to show how you can extend the class generated by the ORM tools. To demonstrate I am using Linq To Sql ORM.

When you make use of ORM tool like Linq to Sql or Entity-Framework it generate the classes from the database structure given as input. For example consider below example
Now I want to display the list of product in my grid but I have to display product name with categoryname for example productname(categoryname) or consider situation where I have ordertable and I have to display one more extra column in grid with display total = quantity * price.

To achieve this make look to the class generated by ORM tools, when you see the class definition you see its decorated with the partial keyword. C#2.0 partial class allow us to create class which expands in two different file and at the time of compile both file get compile in one class. So by making use of same rule I have added one more class file and its partial as below.
public partial class Product
   public string ProductWithCategory
         return this.ProductName + "(" + this.Category +")";
Now to we can consume property in the presentation or businesslayer like as below.
var productlist = (from p in context.Products select p).ToList();
foreach (Product p in productlist)
So by above way adding property to partial class we can easily achieve the task.

By using partial class we can add the custom logic to the class created by the ORM tool(s).


  1. This detailed step-by-step explanation helps a lot of people. Databases are the key of new systems. I´m going to Argentina the next month and the only thing I know there is going to be in the apartment in buenos aires I booked is that there is a computer. I hope I will still use all of the programs I have installed!

  2. Have you ever thought about including a little bit more than just
    your articles? I mean, what you say is important and everything.
    But just imagine if you added some great pictures or video
    clips to give your posts more, "pop"! Your content is excellent but with pics and videos, this site could undeniably be one of the very best in its field.
    Awesome blog!

    Stop by my blog :: garcinia uses

  3. It's nearly impossible to find educated people for this topic, but you sound like you know what you're talking about!


    my webpage ... green coffee beans extract uk

  4. Link exchange is nothing else except it is simply placing the other person's website link on your page at appropriate place and other person will also do similar in support of you.

    my page green coffee bean extract scam

  5. I was wondering if you ever considered changing the layout of your website?
    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect with it better.
    Youve got an awful lot of text for only having 1 or two pictures.
    Maybe you could space it out better?

    my site - benefits of moringa

  6. If you desire to take a great deal from this post then you have to apply these techniques to
    your won website.

    Here is my site :: light teeth whitening

  7. It's genuinely very complex in this busy life to listen news on TV, therefore I only use web for that purpose, and obtain the latest news.

    Here is my weblog; planta de moringa

  8. Whoa! This blog looks just like my old one! It's on a totally different subject but it has pretty much the same page layout and design. Great choice of colors!

    my blog post: teeth whitening

  9. Great beat ! I wish to apprentice while you amend your
    site, how can i subscribe for a blog website? The account helped me a acceptable deal.
    I had been tiny bit acquainted of this your broadcast provided bright clear concept

    Here is my web-site :: tanda pearl ionic teeth whitening system

  10. Definitely consider that that you stated. Your favorite reason appeared
    to be at the web the simplest thing to be mindful of.
    I say to you, I certainly get irked while other folks consider worries that they plainly don't understand about. You controlled to hit the nail upon the top and outlined out the entire thing without having side effect , other people can take a signal. Will probably be back to get more. Thanks

    Feel free to visit my web page: raspberry ketones

  11. Hey there just wanted to give you a quick heads up and let you know a few of the images aren't loading properly. I'm not sure why but I think its a linking issue.

    I've tried it in two different browsers and both show the same results.

    My site ... moringa olifera

  12. Howdy would you mind stating which blog
    platform you're using? I'm planning to start my own
    blog soon but I'm having a tough time deciding between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then most blogs and I'm looking for something completely
    unique. P.S My apologies for getting off-topic but
    I had to ask!

    Here is my website :: garcinia quaesita fruit