More Linq-to-Entities – Simple create,edit, delete

by Pascal Parent 21. July 2009 16:30

I was searching the net for a simple example of a create,edit, delete with Linq-to-Entities, I eventually found but it was too elaborate, so I thought I would give my 2 cents worth and give a simple example. When I embarked on my learning mission of Linq-to-Entities it was to simplify my life, sometimes I wonder if this was the case, I still love Linq though.

Creating a new record is as easy as this:

using (DataEntities db = new DataEntities())
{
//Create a new NewsItem
var newsItem = new NewsItem();
//Assign values	
newsItem.Headline = this.txtHeadline.Text.Trim();
newsItem.PublishedDate = Convert.ToDateTime(this.txtPublishDate.Text.Trim());
newsItem.Active = this.cbActive.Checked;
newsItem.Synopsis = this.txtSynopsys.Text.Trim();
newsItem.Content = this.edContent.Text;
newsItem.Author = this.txtAuthor.Text.Trim();
//Add the new item to the set 
db.AddToNewsSet(_news);
//Commit the changes
db.SaveChanges();
}

Editing a record is slightly more complicated, first you have to have the record if it is not persisted as is the case in ASP.NET.

using (DataEntities db = new DataEntities())
{
//Get the NewsItem for editing
var newsItem = ((from NewsItem in db.NewsSet
where NewsItem.ID == newsID
select NewsItem)
.FirstOrDefault());
//Assign values	
newsItem.Headline = this.txtHeadline.Text.Trim();
newsItem.PublishedDate = Convert.ToDateTime(this.txtPublishDate.Text.Trim());
newsItem.Active = this.cbActive.Checked;
newsItem.Synopsis = this.txtSynopsys.Text.Trim();
newsItem.Content = this.edContent.Text;
newsItem.Author = this.txtAuthor.Text.Trim();
//Commit the changes
db.SaveChanges();
}

Lastly, deleting a record

using (DataEntities db = new DataEntities())
{
//Get the NewsItem for editing
var newsItem = ((from NewsItem in db.NewsSet
where NewsItem.ID == newsID
select NewsItem)
.FirstOrDefault());
//Set the record for deletion from the database
db.DeleteObject(newsItem); 
//Commit the changes
db.SaveChanges();
}

As can be seen there are no complications and Linq just does it.

In my next Linq article I will be talking about relational data editing.

Tags: , ,

ASP.NET | Database

Variable naming conventions in C# (and VB)

by Pascal Parent 21. June 2007 00:06

Now here is an argument waiting to happen and, as you may have figured out, I love a good argument ...

My take on variable naming conventions in C# is as follows:

  1. Use of an underscore for any class scoped variables (private) which allows you to use the same name (save for the Underscore) for your properties.
  2. Use Camel Case
  3. Use meaningful descriptive names so that any other developer can understand what it contain.

It makes it easy to read and remember how to code.

Simplify.

But if you really feel this is way too simple, here is a list of resources:

Or simply search Google.

The point is that no one really agrees.

Tags: ,

General

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010 The ASP.NET Guy