.Net Entity Framework Example

Entity Framework ของ .Net เป็น ORM ที่ใช้งานได้ง่ายดีครับแต่ Tool Wizard ต่างๆทำให้มี Code ที่ถูกซ่อนเยอะแยะเลยหวั่นใจจริงๆ

public static void Main(){
 using (var em = new MyEntities())

                Material m = new Material();
                m.Name = "Material 3";
                m.Price = 436;
                m.GrpId = 1;

                em.AddObject("Materials", m);
                Console.WriteLine("Add mtrl id {0}",m.MtrlId);


                /* Native Query */
                ObjectQuery<DbDataRecord> results = new ObjectQuery<DbDataRecord>("SELECT m.MtrlId,m.Name as Name,m.Price FROM Materials as m", em);
                Console.WriteLine("result= " + results);
                foreach (var item in results)
                    Console.WriteLine("result= Id {0}, Name {1}", item[0], item["Name"]);

                /* LINQ */
                var query = from mtrl in em.Materials
                            select mtrl;
                foreach (var item in query) {
                    Console.WriteLine("LINQ result Id {0}, Name {1}, Price {2}", item.MtrlId, item.Name,item.Price);
} //end main method

About WK

Kajook is a Senior Software Engineer. Job is Design & Develop in Java EE environment on Glassfish. I'm Interested in Agile software development ,iOS and Android.
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s