Nick
Nick

Reputation: 19664

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!

Upvotes: 5

Views: 6562

Answers (2)

mymex1
mymex1

Reputation: 148

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!

Upvotes: 2

Jon Galloway
Jon Galloway

Reputation: 53115

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

Upvotes: 6

Related Questions