ASP.NET MVC 5 Identity with First Model

I am developing a web application and I would like to implement Identity. I've run some tests and it's pretty easy to implement using a Code First approach.

My problem is that I cannot get it to work with the Model First approach. I would like to define my models in a .edmx file and generate my database using the "Create database from model" option.

Here's what I did:

I started with the default ASP.NET Web Application template, including "Individual User Account" authentication. Then I registered a user so that the entity structure would create the required tables in the default database. Then I added "ADO.NET Entity Data Model" to my project and chose "EF Designer from Database" option. It generated successfully with existing tables created by Identity. I changed the connection string in IdentityModels:

public ApplicationDbContext()
        : base("MyConnectionStringName", throwIfV1Schema: false)
{
}

      

But after that, when I try to register a user, I get the following error:

The specified metadata resource could not be loaded.

I also had the error earlier: "The ApplicationUser object type is not part of the model for the current context."

Can Identity Really Be Used With A Model First Approach? If so, what am I doing wrong?

+3


source to share


1 answer


I said I would post something to explain how I got it done. I've struggled to find documentation and tutorials about using Identity with a Model First approach, so here it comes.

First, I found this ingenious article from Ben Foster: http://benfoster.io/blog/aspnet-identity-stripped-bare-mvc-part-1

This article will show you how to implement "Identity Stripped Bare". Reading and following steps allowed me to understand how to use authentication from scratch (you can see in the beginning that it creates an MVC 5 project without authentication). If you want the code for this Naked Identity, here it is: https://github.com/benfoster/NakedIdentity

The first step was to create a database. The Naked Identity code uses v1.0 Identity. Some things are different (like the properties of a pair), but it remains mostly identical. To create the database used by Identity, I simply ran the MVC 5 project, registered a user for the table that would be created using the Code First approach, and then copied over those tables that Identity needed in my empty database.

Once that was done, I created my .edmx using the database I just created. The connection string is added to the web.config file. The connection string may already exist as "DefaultConnection". You must keep this as well.

Basically, these 2 connection strings are needed because Identity will use the default and .edmx will use the other. Can't use the same for both as the .edmx connection string needs metadata that is not supported by identity. So these 2 connection strings are different from each other, but I modified them so that they can refer to the same database.

<connectionStrings> <add name="DefaultConnectionString" connectionString="Data Source=(LocalDb)\v11.0; Initial Catalog=DatabaseName; Integrated Security=True" providerName="System.Data.SqlClient" /> <add name="EntitiesConnectionString" connectionString="metadata=res://*/Models.WebAppModel.csdl|res://*/Models.WebAppModel.ssdl|res://*/Models.WebAppModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDb)\v11.0;initial catalog=DatabaseName;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> </connectionStrings>



The first connection string is the one you are going to use with the entity. So, this is how IdentityDbContext is initialized:

`` public class AppDbContext: IdentityDbContext {public AppDbContext (): base ("QualityWebAppDbEntitiesDefault", throwIfV1Schema: false) {}

    public static AppDbContext Create()
    {
        return new AppDbContext();
    }
}

      

`` (you can find AppUser definition in NakedIdentity sources)

And for my .edmx the initialization looks like this:

public partial class QualityWebAppDbEntities : DbContext { public QualityWebAppDbEntities() : base("name=QualityWebAppDbEntities") { } }

Hope this helps people who want to use Model First approach with Identity.

+3


source







All Articles