Recently, I tried to build an MVC4 application using the SimpleMembershipProvider offered by the Internet Application template. Unfortunately I ran into a slew of compiling errors, run-time errors, and database connection errors once I added in my Entity Framework entities. After tracing down all the issues, I was able to get a clean-slate solution with the SimpleMembershipProvider and Entity Framework working side-by-side in MVC4. Assumptions

  1. EntityFramework has been installed.
  2. The SimpleMembershipProvider files are in the project correctly. Such as…
    1. InitializeSimpleMembershipAttribute.cs
    2. AccountController.cs
    3. AccountModels.cs

Database Setup First, make sure your table has a UserProfile table set up using the following script. Once this is set up, the membership provider will do the rest of the legwork (creating the rest of the tables it needs) for you. Kerrry1

/****** Object:  Table [dbo].[UserProfile]    Script Date: 9/20/2013 10:24:48 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[UserProfile](
	[UserId] [int] IDENTITY(1,1) NOT NULL,
	[UserName] [varchar](75) NOT NULL,
 CONSTRAINT [PK_UserProfile] PRIMARY KEY CLUSTERED 
(
	[UserId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

Note: If you do not set the UserId as the primary key, you can get errors like this:

  1. The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588

Delete the UsersContext and Code-First Models

We will no longer need the UsersContext when we have our Entity Framework context created, so we can just remove it. We also want to remove the UserProfile code-first class to avoid errors such as #1 (Missing partial modifier on declaration of type ‘MvcApplication1.Models.UserProfile’; another partial declaration of this type exists). Kerry2

Creating the Entities and Connection String

Now, add your new entities through EntityFramework through the Add -> ADO.NET Entity Data Model wizard (IMAGE http://d.pr/i/A5sg). This will create the class files for your objects as well as generate a new connection string into the Web.config with which EntityFramework uses to create a context.

Setting SimpleMembershipProvider’s Context

If you try to build, you will get some errors such as “The type or namespace name ‘UsersContext’ could not be found (are you missing a using directive or an assembly reference?)”. Track all of these down and replace UsersContext with the name of your database context (“MyEntitiesContext” or whatever you named it). There will be 2 uses in Controllers/AccountController.cs and 2 uses in Filters/InitializeSimpleMembershipAttribute.cs.

Modifying the DefaultConnection String

The DefaultConnection string is the one used by the SimpleMembershipInitializer. You can change the name of this on (by default) line 33 of Filters/InitializeSimpleMembershipAttribute.cs: WebSecurity.InitializeDatabaseConnection(“DefaultConnection”, “UserProfile”, “UserId”, “UserName”, autoCreateTables: true); Kerry3 NOTE: You cannot use the same connection string as the Entity Framework connection string. If you do, you will see errors like this:

  1. The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588

Instead, you need to pull out the “provider connection string” value in the Entity Framework connection string, without the “App=EntityFramework” parameter. For example:

    <add name="DefaultConnection" connectionString="data source=(local);initial catalog=actest;user id=sa;password=[mypassord];MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
    <add name="MyEntities" connectionString="metadata=res://*/Models.Entities1.csdl|res://*/Models.Entities1.ssdl|res://*/Models.Entities1.msl;provider=System.Data.SqlClient;provider connection string="data source=(local);initial catalog=actest;user id=sa;password=[mypassword];MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
	

You are now free to use Entity Framework! You can download the source code at my GitHub repository. Bonus note: To resolve this error, add a primary key to your entity and re-generate the model. Unable to update the EntitySet [your_entity] because it has a DefiningQuery and no <InsertFunction> element exists in the element to support the current operation. Check out our .NET Development page for more ways we can help you!