Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I integrate Membership tables with Entity Framework? Asp.net

I have never used Entity Framework in a project before so I am not sure where to begin. I have I am using the membership tables that are created when using ASP.Net membership provider as is.

I would like to create an object that contains a reference to a user. For example

public int Id {get;set;}
public User User{get;set} // Where User is some object relating to Membership provider User.
public string Application {get; set;}

I am using a code first approach to using EF so I would like to let it generate tables based on this object. So how do I configure this so that EF will recognize that I am creating a relationship to a user managed by asp.net Membership? What type should I use in the above example in place of 'User' (what is the asp.net type)

Sorry if this is vague. I basically just need an example or explanation of how EF integrates with Asp.net membership provider schema.

Thanks!

like image 397
Nick Avatar asked Jun 22 '11 22:06

Nick


2 Answers

If you want to have one EF model which links your membership tables and application tables, you can add the ASP.NET Membership tables to your existing database using the aspnet_regsql.exe utility. Some links:

  • MSDN documentation
  • Tutorial on the ASP.NET website

Once you have the Membership tables installed, you can generate EF Code classes that match the membership schema. I posted a detailed walkthrough on how to do that here:

Generating EF Code First model classes from an existing database

like image 161
Jon Galloway Avatar answered Oct 18 '22 11:10

Jon Galloway


I would say you could create an "AppUser"(Your own custom User object) Entity where the corresponding table has a foreign key to the "UserID" column of the aspnet_Membership table. This way it will be easier if you need to change or add properties to your "AppUser" Entity instead of trying to change the MS table structure (which can be a real pain). You can still interact with the built-in MS Membership classes and functions from your MVC project using something like the MvcMembership starter Kit DLL's.

https://github.com/TroyGoode/MembershipStarterKit

Hope this helps!

like image 36
mymex1 Avatar answered Oct 18 '22 11:10

mymex1