MVC4 using db first with simplemembership
I have my membership working to the point where I can save a a new user. Once the person is registered, I need to render a new view so they can fill in more information, so I need the newly created userid from the UserProfile table to be passed to the new view. I have tried modifying the templated register method to this:
try
{
WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { Email = model.Email }, true);
WebSecurity.Login(model.UserName, model.Password);
//Get the userid for this new user
var db = new UsersContext();
int id = db.UserProfiles.Where(x => x.UserName == model.UserName).Select(x => x.UserId).FirstOrDefault();
return RedirectToAction("Edit", "Profile", id);
}
This gives me this error:
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'UserProfile' has no key defined. Define the key for this EntityType. \tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'UserProfiles' is based on type 'UserProfile' that has no keys defined.
I believe this error has something to do with trying to use EF on a table that is not in an edmx model. I have not added any of the simplemembership tables (UserProfile, Membership, Roles, etc) into my edmx file because when I do that I get errors of duplicate classes, I'm guessing because the SM tables are created code first.
I have never done any code first, does the answer lay there? Or do I need to add the SM tables to my edmx model so I can query them. If so, how do I get around teh duplicate class error?
I have the 2 seperate connection strings in my web config.
EDIT++++++++++
I have been researching and found a different way, but I get the same error.
int id;
using (var db = new UsersContext())
{
var user = db.UserProfiles.Find(model.UserName);
id = user.UserId;
}
I get the error on the 'using' line. Here is the code for the UsersContext:
public class UsersContext : DbContext
{
public UsersContext()
: base("SecurityEntities")
{
}
public DbSet<UserProfile> UserProfiles { get; set; }
}
"SecurityEntities is the connection string which looks like:
<add name="SecurityEntities" connectionString="data source=(localdb)\v11.0;initial catalog=OurAgreements;user id=oaadmin;password=136VDSyPLrcfgPfw3BIH;multipleactiveresultsets=True;application name=EntityFramework" providerName="System.Data.SqlClient" />
Can you see where I am going wrong?