ADO.NET Linq to Entities versus Linq to SQL

I recently did a project using LINQ to SQL. I decided to convert it to the ADO.NET Linq to Entities model instead. It was fairly easy to convert with a few gotchas. I will list the changes here:

Linq to SQL ADO.NET Linq to Entities
Creating a Model:
Add New Item: Linq to SQL Classes
Creating a Model:
Add New Item: ADO.NET Entity Data Model
query = _
(From m In dc.Menu _
Where m.menuId = menuId _
Select m).SingleOrDefault()
SingleOrDefault is not supported in the current Entities releases (talk has it that it will be added in future releases). So use FirstOrDefault() in your Linq to SQL if you want your code to be forward compatible with Entities.
query = _
(From m In dc.Menu _
Where m.menuId = menuId _
Select m).FirstOrDefault()
Linq to SQL Deletion and Submitting Changes:
            
Dim dc As New MyEntities()
Dim menuDetail = _
(From md In dc.Menu _
Where md.menuId = menuId _
Select md).FirstOrDefault
If Not menuDetail Is Nothing Then
    dc.Menus.DeleteOnSubmit(menuDetail)
    dc.SubmitChanges()
End If
Linq to Entities Deletion and Submitting/Saving Changes:
               
Dim dc As New MyEntities
Dim menuDetail = _
(From md In dc.Menu _
Where md.menuId = menuId _
Select md).FirstOrDefault
If Not menuDetail Is Nothing Then
  dc.DeleteObject(menuDetail)
  dc.SaveChanges()
End If
dc.Menu.InsertOnSubmit(myRecord)
dc.AddToMenu(myRecord)

Also, the Linq to SQL messes with the "s" on the end of table names and Linq to Entities does not. 

If I've made some mistakes, feel free to educate me. :)

Thanks to the Experts at Experts-Exchange for their help!

May your dreams be in ASP.NET!

Nannette Thacker

 

4 Comments

  • Hi!

    That's a pretty informative article. I've been thinking about trying out the Entity Framework, but never got about really investigating it yet.

    Currently, I just use a mix of LLBLGen and Linq2Sql.

    -Nitin

  • Here is a useful post:

    http://blogs.msdn.com/adonet/archive/2008/12/02/migrating-from-linq-to-sql-to-the-entity-framework-stored-procedures-for-data-retrieval.aspx

    Migrating from LINQ to SQL to the Entity Framework: Stored Procedures for data retrieval

    Of course, all it's really saying is that linq to entities is so limited and crippled that you may as well forget using it the way it is currently released.

    It certainly has been a nightmare for me trying to get anything done with it.

  • Tentome,

    If you are using stored procedures other than insert/update/delete, EF is definitely not there yet.

    I've been trying to get a stored procedure to run that doesn't return a result and it has been a nightmare.

    Go here for blogs on the ado.net EF:
    http://blogs.msdn.com/adonet/default.aspx

    This article on whether Linq to SQL is dead, is interesting:
    http://www.infoq.com/news/2008/11/DLINQ-Future

    A key comment voiced by this author is:

    "If you read this literally, it merely says that Entity Framework will get more development resources than LINQ to SQL. The problem is Microsoft has a long history of deprecating data access technology without outright saying it is no longer supported."

    I personally have not found a whole lot of help with the Linq to Entities, particularly with using stored procedures.

    If I run a stored procedure, even attaching it to my entities model, then I run a linq command to retrieve the data, the changes made in the stored procedure have not been updated in the entities framework, and finding a solution to "fix" that... well, I haven't found one, in spite of numerous forum posts.

    So I rewrote all of my code with an ADO.NET version, without Linq. I could go back to Linq to SQL, but it seems a waste of time if they're going to pull the plug on it and seem to be moving forward with Linq to Entities instead. They keep talking of 2010. Well, I guess I'll wait til 4.0 and 2010 are released before really getting excited about getting back into Linq to Entities. I've already wasted enough time on it.

    Nannette

  • http://forums.asp.net/p/1363046/2857159.aspx#2857159

    To quote simmdan:

    "The Entity Framework support for read stored procedures is not as full featured as we would like in the first release of the product. As was mentioned above you can have stored procedures which will read from the database but the built in support requires that those procedures return entities--if you want to have a stored procedure that returns some other structure, things get more complicated. This and a number of other deficiencies in this area will be resolved in the next release of the entity framework which will ship with .net 4.0/vs2010."

Comments have been disabled for this content.