Ego Placebo
Ego Placebo

Reputation: 168

Use existing aspnet membership schema with new ASP.NET MVC database first

I'm looking for some guidance here... I have an old ASP.NET webforms site with an existing database and SQL Server membership, and I'm creating a new interface using ASP.NET MVC and Entity Framework with a database-first approach.

So I have tables as shown below:

membership tables - how to use in MVC?

My problem is, I'm unsure how to make use of these old membership tables for authentication in the new ASP.NET MVC application.

I don't mind whether I migrate the user/roles data to ASP.NET Identity or continue using the old membership database but I'm not sure what the best approach is. I think I would prefer to migrate the data, as I am more familiar with Identity.

What I've tried...

While searching for solutions I have come across this: Migrating an Existing Website from SQL Membership to ASP.NET Identity several times, however this (and most of what I've found so far) is talking about upgrading an existing webforms site to use asp.net identity. I have tried using parts of it to migrate the Membership data to asp.net identity, but of course the remaining instructions don't fit my scenario.

So here's where I'm at now.

  1. I have used the SQL script in the walkthrough linked above to migrate membership data to asp.net identity tables.

  2. I have set up SQLPasswordHasher as described in the walkthrough, since I figure I'll need that function to use existing logins.

  3. I imported those tables to the database that contains the website data, in hopes that I'd only have to deal with a single connection string.

  4. I modified ApplicationDbContext : base... to use that database (it was previously using DefaultConnection (LocalDb)

So now I have these tables populated with data:

enter image description here

Aaand I'm stuck.

It's entirely possible that I'm missing something basic, but anyway if I try to login or register a new user in it's current state I get the error:

The entity type ApplicationUser is not part of the model for the current context.

on the line:

var result = await UserManager.CreateAsync(user, model.Password);`

The only solutions I've been able to find for that error have been on code-first projects: something to do with migrations, which I'm not using (I did try it but received an error on enable-migration "Creating a DbModelBuilder or writing the EDMX from a DbContext created using database-first or model-first is not supported.")

UPDATE Ok so I came across an answer regarding the The entity type ApplicationUser is not part of the model for the current context. error, apparently using the connection string generated by entity framework can be problematic, so as advised, I made a 'normal' connection string and this seems to be getting closer!

UPDATE 2

I think I have it working, will run some tests though before updating with my solution.

Upvotes: 0

Views: 865

Answers (1)

Praveen Maurya
Praveen Maurya

Reputation: 296

Please follow the below step to do this.

  1. Create a new ASP.NET MVC Project with user authentication.
  2. Now right click on Model folder and add a new item "ADO.NET Entity Data model" enter your model name and select ok now select Database first approach and provide your database name and connection and connection name to be saved in web.config file.
  3. Now go to your web.config file and you will see two connection are defined in your connection string and each connection point to different database , one for IDENTITY and other for which you have added just now. for eg: enter image description here
  4. you need to change the data source of the both connection to be same.
  5. So whenever you need to access user info you need to use ApplicationDbContext and for others you can use you 'CustomConn' context which you have defined earlier while adding new model.

Hope this will help you !

Upvotes: 1

Related Questions