Back to basic (Part 1): Adding single row / record using LINQ to Entities

Create a sample database named Beancurd in SQL Server. (Get SQL Express 2008


Switch to Beancurd database

USE Beancurd

Create  a table Beancurds table in Beancurd database


(BeancurdID intPRIMARYKEY,

BeancurdType NVARCHAR(50),

Qty int


Start Visual Studio 2008.

1.       Choose File -> Projects…

2.       Choose Console Application in C#. Type any name.

3.       From the pull down menu choose Project -> Add New Item…

4.       Choose Data from Categories pane and ADO.NET Entity Data Model. Type any name then click the Add button.


5.       Select Generate from database

6.       Choose New Connection…

7.       Select the Beancurd database you have created in previous steps


8.       Click Next

9.       Click the + sign to expand the table and choose Beancurds table then click Finish




10.   Replace the main function as below:

    staticvoid Main(string[] args)


      //Define an entity object

      BeancurdEntities entBC = newBeancurdEntities();

      //define a single Beancurds object

      Beancurds objBC = newBeancurds();

      //Assign value to columns

      objBC.BeancurdID = 1;

      objBC.BeancurdType = “China”;

      objBC.Qty = 10;

      //Add to database




Download the complete code from The sample file name is ConAppLINQtoEF.rar. My MSN ID is



About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress ( till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net and tagged , , , . Bookmark the permalink.

8 Responses to Back to basic (Part 1): Adding single row / record using LINQ to Entities

  1. Pingback: How to: Bracket Data Submissions by Using Transactions (LINQ to Entity) | Chanmingman's Blog

  2. Pingback: Passing Entity Framework edmx over WCF Services, using array at receiving end | Chanmingman's Blog

  3. Pingback: Change the connection string dynamically for EntityDataSource during run time | Chanmingman's Blog

  4. Pingback: Change the connection string dynamically for edmx entity during run time | Chanmingman's Blog

  5. Pingback: Add a simple report to ASP.NET Web Application using Report Wizard in Visual Studio 2010 – (Video) | Chanmingman's Blog

  6. Pingback: Navigate from report to report in ASP.NET using URL and chart – sample | Chanmingman's Blog

  7. Pingback: Navigate from one rdlc report to another filter through QueryString using ReportViewer – sample | Chanmingman's Blog

  8. Pingback: Back to basic (Part 2): Read from LINQ to Entities | Chanmingman's Blog

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s