Asp.net mvc 4, all the tutorials show simple membership being utilized with Code First (latest trend in database setup/usage). This is how to utilize simple membership with Database First using Entity Data Model.
This was discovered via a StackOverFlow.com answer: http://stackoverflow.com/questions/15112214/using-mvc-4-simplemembership-with-an-existing-database-first-ef-model
1) Create MVC 4 app and run it to create the default membership database and tables (mvc does this automatically).
2) Change .../Filters/InitializeSimpleMembershipAttribute.cs as follows:
private class SimpleMembershipInitializer
{
public SimpleMembershipInitializer()
{
//Database.SetInitializer<UsersContext>(null);
try
{
//using (var context = new UsersContext())
//{
// if (!context.Database.Exists())
// {
// // Create the SimpleMembership database without Entity Framework migration schema
// ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
// }
//}
if (!WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
}
}
catch (Exception ex)
{
throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
}
}
}
3) Comment out sections in /Models/AccountModels.cs:
public class UsersContext : DbContext
{
public UsersContext()
: base("DefaultConnection")
{
}
//public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<ExternalUserInfo> ExternalUsers { get; set; }
}
//[Table("UserProfile")]
//public class UserProfile
//{
// [Key]
// [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
// public int UserId { get; set; }
// public string UserName { get; set; }
//}
4) Create an entity data model (*.edmx file) as normal.
5) In /Controllers/AccountController.cs and anywhere else where DB interaction is needed, replace as follows:
//using (UsersContext db = new UsersContext())
using (evEntities ent = new evEntities())
{
// code to access database
}
No comments:
Post a Comment