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