2
votes

I'm trying to use my own Users table for simplememership, adding some data to it. based on the sample here - http://blog.osbornm.com/2010/07/21/using-simplemembership-with-asp.net-webpages/ I've created a table with this model:

 [Table("MyUsers")]

public class MyUsers

[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string Gender { get; set; }
public DateTime DOB { get; set; }

I'm initializing it :

  WebSecurity.InitializeDatabaseConnection("MyConnection", "MyUsers", "UserId", "UserName", autoCreateTables: true);

and on registration I call:

  WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new MyUsers {DOB = DateTime.Now.AddYears(-31),Email = "[email protected]",Gender = "Male",UserName = model.UserName});

But - I get this error: Cannot insert explicit value for identity column in table 'MyUsers' when IDENTITY_INSERT is set to OFF.

not sure how to set this off in VS2010, but I'm not sure why I even get this error, ans what should I fix.

Help appreciated, I'm trying to test if i can move a legacy asp.net project to MVC.

Thansk

2

2 Answers

4
votes

Found my issue - it was with CreateUserAndAccount, I used a class that matched all fields in the table, including the key. shouldn't be this way. this works:

 WebSecurity.CreateUserAndAccount(model.UserName, model.Password,new  {Gender="Mal",DOB = DateTime.Now.AddYears(-1),Email="[email protected]" });

Thanks.

0
votes

What you've done is built an extension off of the base users table. Because of this the UserId column should not be an auto incremented column. It's going to try and INSERT the actual ID from the base users table as the foreign key.