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.
Summary
By using partial class we can add the custom logic to the class created by the ORM tool(s).
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
{
get
{
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)
{
Console.WriteLine(p.ProductWithCategory);
}
So by above way adding property to partial class we can easily achieve the task.Summary
By using partial class we can add the custom logic to the class created by the ORM tool(s).

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!
ReplyDeleteAmy
Have you ever thought about including a little bit more than just
ReplyDeleteyour 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
It's nearly impossible to find educated people for this topic, but you sound like you know what you're talking about!
ReplyDeleteThanks
my webpage ... green coffee beans extract uk
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.
ReplyDeletemy page green coffee bean extract scam
I was wondering if you ever considered changing the layout of your website?
ReplyDeleteIts 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
If you desire to take a great deal from this post then you have to apply these techniques to
ReplyDeleteyour won website.
Here is my site :: light teeth whitening
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.
ReplyDeleteHere is my weblog; planta de moringa
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!
ReplyDeletemy blog post: teeth whitening
Great beat ! I wish to apprentice while you amend your
ReplyDeletesite, 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
Definitely consider that that you stated. Your favorite reason appeared
ReplyDeleteto 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
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.
ReplyDeleteI've tried it in two different browsers and both show the same results.
My site ... moringa olifera