Is it possible to remove MVC 4's default dependence on Entity Framework? dapper entity-framework


Is there a way to remove the default dependency on Entity Framework within an ASP.NET MVC 4 project and replace it with another similar technology such as Dapper

3/30/2013 1:48:04 AM

Accepted Answer

If you create a new ASP.NET MVC 4 Web Application and choose "Internet Application" as the project template you will notice that Entity Framework is referenced and used as part of SimpleMembership. However, there is only a very small dependency on Entity Framework and it probably isn't worth removing it.

Entity Framework appears to be used for just 2 minor tasks

The first way in which Entity Framework is used is to create the database schema needed to store the membership data. Database schema creation is not something that Dapper will do for you and if you remove Entity Framework you will have to manually manage changes to your membership models/database.

The second way in which Entity Framework is used is within a single method named "ExternalLoginConfirmation" inside the default "AccountController" as part of it's OAuth integration. It is used to register new users that have been authenticated from an external provider (like Facebook).

We can say then that SimpleMembership uses SQL commands and not Entity Framework [1].

Because SimpleMembership uses SQL commands and not Entity Framework it should be as fast as a comparable Dapper solution for this task. In addition, this configuration of SimpleMembership has been tested extensively by Microsoft and the community. For those reasons I would leave it alone. Code that deals with security should be left to accredited security experts [2].

How to remove Entity Framework from a new ASP.NET MVC 4 Web Application

If you really want to, the dependency is very easy to remove (I am assuming your membership database is already created and working).

=====> Step 1

In the Visual Studio Solution Explorer open up the "References" folder.

Right-click on "EntityFramework" and select "Remove".

=====> Step 2

Open Filters/InitializeSimpleMembershipAttribute.cs

Delete the following:

using System.Data.Entity.Infrastructure;


Database.SetInitializer<UsersContext>( null );


using ( var context = new UsersContext() ) {
    if ( !context.Database.Exists() ) {
        // Create the SimpleMembership database without Entity Framework migration schema
        ( ( IObjectContextAdapter ) context ).ObjectContext.CreateDatabase();

=====> Step 3

Open: Models/AccountModels.cs

Delete the following:

public class UsersContext : DbContext {
    public UsersContext()
        : base( "DefaultConnection" ) {

    public DbSet<UserProfile> UserProfiles {

=====> Step 4

Open: Controllers/AccountController.cs

Look for the method named "ExternalLoginConfirmation".

Replace the following:

using ( UsersContext db = new UsersContext() ) {
    UserProfile user = db.UserProfiles.FirstOrDefault( u => u.UserName.ToLower() == model.UserName.ToLower() );
    // Check if user already exists
    if ( user == null ) {
        // Insert name into the profile table
        db.UserProfiles.Add( new UserProfile {
            UserName = model.UserName
        } );

With your dapper equivalent code -- the code-comments tell you what needs to be implemented.

You should be able to remove this and other methods altogether if you are not using OAuth.

Et Voila ;)

[1] "Everything's implemented as SQL calls rather than requiring stored procedures, views, agents, and change notifications."
Jon Galloway, Microsoft

[2] "Let me give you all my standard caution about rolling your own cryptographic algorithms and security systems: don't. It is very, very easy to create security systems which are almost but not quite secure. A security system which gives you a false sense of security is worse than no security system at all!"
Eric Lippert, Legend

3/29/2013 11:56:18 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow