I was making an internet MVC 4 application in Visual Studio 2012 and I wanted to add users using SingleMembership. At first I saw that the users database was not exactly where I wanted them to be, they were on a SQL/Express database and I want them on a SQL/Server I have already created for all the models in my application. So what I did is modify the connection string created by Visual Studio 2012 for my database which was:
<add name="Test2_Entities" connectionString="metadata=res://*/Models.Model.csdl|res://*/Models.Model.ssdl|res://*/Models.Model.msl;provider=System.Data.SqlClient;provider connection string="data source=FERNANDO-PC\SQLSERVER;initial catalog=Test2;persist security info=True;user id=****;password=****;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
and then I realized I needed 2 connection strings because the metadata part was giving some errors and the provider name change it to "System.Data.SqlClient", so the new connection string is really the first one but with this changes:
<add name="Test2_Entities2" connectionString="data source=FERNANDO-PC\SQLSERVER;initial catalog=Test2;persist security info=True;user id=****;password=****;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
In otder to have the users tables created in my database, I also changed a line in the InitializeSingleMemebershipAttribute.cs file in the Filters folder (to be precise, the line 41) from:
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
to:
WebSecurity.InitializeDatabaseConnection("Test2_Entities2", "UserProfile", "UserId", "UserName", autoCreateTables: true);
and other line in the AccountModels.cs which is in the Models folder from:
public UsersContext() : base("DefaultConnection")
to:
public UsersContext() : base("Test2_Entities")
Whit that the default tables which control the memberships, roles, OAuth and those things, are created in the database I want. (This is the background of the problem, and thought that some people would find it helpful)
The problem is that when the tables are created in my SQL database, there are columns that are not in the datatypes I defined in the AccountModels.cs and the additional fields are not created. More specifically, in the AccountModels.cs I have this class:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public Guid UserId { get; set; }
public string UserName { get; set; }
public string RandomString { get; set; }
}
and in the tables created, the UserID gets an int which when I try to change in I get an error:
Conversion from 'int' to 'uniqueidentifier' is not supported on the connected database server.
and also, the RandomString string does not appear on the UserProfiles table.
The answer to this question: "How do I manage profiles using SingleMembership" stackoverflow question is really helpful, but I can't change the fields and datatypes like the answer proposes.
Is there something I'm missing? or something I should set before doing this changes?