How can I use Entity Framework with Membership tables? Asp.net

asp.net asp.net-mvc entity-framework

Question

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!

1
5
6/22/2011 10:28:00 PM

Accepted Answer

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

6
6/23/2011 3:28:00 AM

Popular Answer

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!



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow