Pete
Pete

Reputation: 10871

EF 4.1 Code First and Membership

I did go through the suggest similar questions and most were very outdated since all the new developments with EF Code First. I'm looking for ideas or some way around this EF Code First "stumbling block".

Is there anyway to use the built in asp membership system with an EF Code First project? One common Code First example is the Blog, Post, Comment like so (from: http://msdn.microsoft.com/en-us/data/gg685467):

public class Blog
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string BloggerName { get; set;}
    public virtual ICollection<Post> Posts { get; set; }
  }

public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public DateTime DateCreated { get; set; }
    public string Content { get; set; }
    public int BlogId { get; set; }
    public ICollection<Comment> Comments { get; set; }
}

public class Comment
{
    public int Id { get; set; }
    public DateTime DateCreated { get; set; }
    public string Content { get; set; }
    public int PostId { get; set; }
    public Post Post { get; set; }
}

This is fabulous except that there is no way to track the user who posted a Comment. Is there anything that can be done to make code first stuff like this work with asp.net membership?

The first thing that comes to mind is something like (just working with Comments here):

public class UserProfile
{
    public Guid Id { get; set; }
    public string FirstName { get; set; }
    public string FavoriteColor { get; set; }
    //Other useless user profile stuff here
}    
public class Comment
{
    //Comment fields same as above
    public UserProfile Owner { get; set; }
}

The first thing that comes to mind is to change the Register action in the Account controller so when a user registers, their Guid (possibly their username as well) is also added to the UserProfile table created by EF Code First. I guess this table would be a sort of bridge between my EF Code First tables and the asp.net membership tables.

Does this approach make any sense? What are some pros/cons and are there any better options?

Upvotes: 0

Views: 1177

Answers (2)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93424

Yes, that's one way to do it. Another is that you don't actually need to access the membership, all you need is the username, which when a user has been logged in via FormsAuthentication will populate the IPrincipal.

var username = User.Identity.Name;

or

HttpContext.Current.User.Identity.Name;

Any other information you may need is accessible from the Membership API.

MembershipUser user = Membership.GetUser(User.Identity.Name);

Don't be tempted to try and map the ASP.NET Membership tables into your data model, as this will cause problems with the default membership provider. Just use the membership API to get any information you need.

Upvotes: 1

Adam Tuliper
Adam Tuliper

Reputation: 30152

Install the aspnet membership items into your database. You won't then be using CodeFirst on those entities. However, you can simply read who the user is from the MembershipProvider. Those tables can be queried directly if you need to (but the membership API should give you what you need) You can also run the 'reverse engineer' tool from the entity framework power tools to give you the entities directly to work with.

You can get more info on installing into your DB here. Using HttpContext.Current.Application to store simple data

The power tools are here to reverse engineer your tables into code first entities once they are added to the db:

http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d

Upvotes: 1

Related Questions