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!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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:

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

Method 2

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!


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x