1
votes

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?

2

2 Answers

1
votes

What does your definition for UserProfile look like. It should look something like this:

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    ...
 }

The Key attribute indicates that the UserId is the key and the DatabaseGeneratedAttribute indicates that the UserId value is generated by the database. The error you are getting seems to indicate you do not have the Key attribute on your UserProfile definition.

Updated 5/29/13

If you are having trouble with changing the schema for UserProfile and it is not setup for migration take a look at this article that shows how to customize and seed SimpleMembership. The setup in this article is geared more towards development and testing your application. There is another article on setting up database migration for SimpleMembership.

0
votes

I tried

WebSecurity.CreateUserAndAccount(model.UserName, model.Password, null, false);
var ID = WebSecurity.GetUserId(model.UserName);

and it works well for me