Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Membership provider with EF Code First?

I have models based on EF Code First and I want to use them with the default MembershipProvider, but I don't know how to write the model correctly, so it won't erase all my data on recreating the tables when there were changes made to the model.

like image 398
Santas Avatar asked Jan 23 '11 15:01

Santas


People also ask

How can I use ASP net membership provider?

The ASP.NET membership provider is a feature that enables ASP.NET developers to create Web sites that allow users to create unique user name and password combinations. With this facility, any user can establish an account with the site, and sign in for exclusive access to the site and its services.

How can we implement membership provider in ASP NET MVC?

Let's create a application for membership provider ASP.NET MVC. Step 1: Go to visual studio and click on new project -> a window will open from here select a 'ASP.NET MVC4 web application' and give the name for this project in my case I give it as “MVCMembershipProvider ".


2 Answers

Have a look at this project

http://codefirstmembership.codeplex.com/

It has entity classes for users and roles, as well as a roleprovider and membershipprovider implementation. If you include the users and roles in your datacontext class the tables will be created in your database.

like image 168
woggles Avatar answered Oct 15 '22 21:10

woggles


Your question has two parts.

  1. How to use asp.net membership API with EF code first?
  2. How to preserve existing data when model changes?

as for How to preserve existing data when model changes, as far as with EF 4.0/ asp.net mvc 3, database migrations are not yet supported. You will have to move to asp.net mvc 4.0/ EF 4.3 where database migrations are supported or use similar alternatives , but its still beta release.

asp.net mvc 4.0 database migration in scott gu's blog

Now coming to the point on how to use asp.net membership provider with EF code first. There are couple of challenges :

  1. We cannot/should not do an join with asp.net membership provider tables. Its not recommended, so my suggestion will be to create a "adapter class" for asp.net membership provider classes. For ex :

     public class UserAdapter
     {
     // all user related attributes. Not stored in membership schema, but your schema
    
       public string UserProxyName;
    
       // some attributes stored in membership schema
       [NotMapped]
       public string Email { 
            get
            {
                  Membership.GetUser(UserProxyName).Email; 
            }                
       }        
    
      // some attributes stored in membership schema and not in your schema
      [NotMapped]
      public string[] UserRoles
      {
        get
        {
            return Roles.GetRolesForUser(UserProxyName);
        }           
      }
    
    }
    

    Now for updating information , you may write some functions in Model itself, however i would suggest create a UserRepository with repository design pattern to handle user CRUD operations.

  2. Second challenge is how to create the database on first run. As seeding becomes an issue, if you want to seed user information then seperately running aspnet_regsql is not efficient as membership schema is expected before the seeding happens. I came across this nice article , with some fine tuning it worked for me :

asp.net membership and EF

like image 34
amarnath chatterjee Avatar answered Oct 15 '22 20:10

amarnath chatterjee