user1477388
user1477388

Reputation: 21430

Confusing MVC and LINQ to SQL

In this tutorial http://mstecharchitect.blogspot.com/2009/08/aspnet-mvc-and-linq-to-sql-using.html they recommend using LINQ to SQL in MVC. They then create a repository which basically makes it look more like the Entity Framework (using methods like Add() and Delete() in place of LINQ's InsertOnSubmit() and DeleteOnSubmit().

My question is, what should I do here? I love using LINQ but I feel it would break MVC if I abandon Entity Framework for LINQ to SQL.

LINQ to SQL generates the following line:

public partial class DataClasses1DataContext : System.Data.Linq.DataContext

So, I can only use methods such as InsertOnSubmit() and I can't use methods from Entity Framework like Add().

It sounds like LINQ to Entities would be the answer, but I am not sure how that comes into play...

I have also seen the comments here Linq.DataContext to Entity.DbContext? which seem to imply that it's bad practice...

Upvotes: 0

Views: 582

Answers (1)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93424

First, notice the date on that article. It's from 2009. That means it precedes Visual Studio 2010 and .net 3.5. The article is talking about Entity Framework 1.0, which had a large number of problems, and thus it was often recommended to use Linq to SQL rather than EF 1.0.

However, as of VS 2010 and EF 4.0, most of those concerns were addressed and EF is now the preferred method over Linq to SQL. L2S is still supported, but Microsoft is no longer doing any major improvements to it.

As others have mentioned in the comments, MVC is database agnostic. You can use any database technology with it, and it is not dependent on EF in any way. You can use EF, L2S, nHibernate, ADO.NET, etc... You won't "break MVC" because MVC has no concept of your database technology.

Many people still implement repositories and Unit of Work on top of EF (although I personally see that as extra work unless you plan to need a database abstraction that allows you to change technologies).

EDIT:

Upon further reading of the article, I don't see anywhere where it recommends L2S over EF, in fact at the end of the article it has this comment:

Suppose If you want to change the data access technology using EF ( Entity Framework) instead of LINQ TO SQL you simply implement the IStudentRepository interface with a class that uses the alternative database access technology

Upvotes: 1

Related Questions