The Acumen software design blog provides you with helpful articles about MVC4.

This web site provides a good overview.

Official Microsoft MVC4 Overview

Recently, while developing an application using Entity Framework, I ran into several compilation errors after adding an additional table to my database. Most notably was this error when trying to declare a couple of variables that had once worked before:

Compiler Error Message: CS1061: … does not contain a definition for … and no extension method … accepting a first argument of type … could be found (are you missing a using directive or an assembly reference?)

When searching around in my .edmx file for reasons as to why my relationships were not working, I ran into this message under the mapping details:

“Mappings are not allowed for an association over exposed foreign keys.”

I wanted to note that this message is not relevant to the issue at hand. After I solved the problem, I still received this message.

I quickly realized that the error had to do with the relationships between tables that I had previously established. When I had added the new table, I deleted the diagram from my .edmx file and updated the model from the database like I normally do when adding a new table. However, this time, Entity Framework did not properly regenerate the code associated with my database. Each property was stored as a class member within the entity class as expected, but my relationships were not. This was despite the fact that my .edmx diagram showed the relationships exactly as they should be.

After hours of Googling different phrases and errors, I ran into this ticket

As it turns out, this is a bug with Entity Framework inside of VS2012, and if your Visual Studio is out of date, you are likely to experience it. Because of this bug, if your .edmx file is nested within a project folder, Entity Framework will sometimes fail to generate code properly. Fortunately, the solution is much easier to implement than it is to find. You can do any of the following:

  • Update your Visual Studios here
  • Right click on the .tt file and select “Run Custom Tool”.
  • Drag the .edmx file out of the project folder.

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!