I am new in MVC with MySQL. I have code first application in MVC 4 with MySQL.
public class Category
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public virtual IEnumerable<Product> Products { get; set; }
}
public class Product
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public DateTime CreateDate { get; set; }
public int CategoryID { get; set; }
public virtual Category Category { get; set; }
}
public class MyContext : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<Category> Categories { get; set; }
}
And in web.config:
<connectionStrings>
<add name="MyContext" connectionString="server=localhost; User Id=root; Pwd=mypass; Persist Security Info=True; database=catalogue" providerName="MySql.Data.MySqlClient " />
</connectionStrings>
So, this creates catalogue database in mysql. But i want also to use membership of mysql. To register, to create users and manage their roles. For example, using default AccountModels of mvc4
I added this code to web.config:
<system.web>
<membership defaultProvider="MySQLMembershipProvider">
<providers>
<clear />
<remove name="MySQLMembershipProvider" />
<add name="MySQLMembershipProvider" type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.5.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" applicationName="/" description="MySQL default application" connectionStringName="LocalMySqlServer" writeExceptionsToEventLog="True" autogenerateschema="True" enablePasswordRetrieval="True" enablePasswordReset="True" requiresQuestionAndAnswer="False" requiresUniqueEmail="False" passwordFormat="Clear" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression="" />
</providers>
</membership>
<profile defaultProvider="MySQLProfileProvider">
<providers>
<clear />
<remove name="MySQLProfileProvider" />
<add name="MySQLProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider, MySql.Web, Version=6.5.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" applicationName="/" description="Profiles" connectionStringName="LocalMySqlServer" writeExceptionsToEventLog="False" autogenerateschema="True" />
</providers>
</profile>
<roleManager enabled="false" defaultProvider="MySQLRoleProvider">
<providers>
<clear />
<remove name="MySQLRoleProvider" />
<add name="MySQLRoleProvider" type="MySql.Web.Security.MySQLRoleProvider, MySql.Web, Version=6.5.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" applicationName="/" description="Project Roles" connectionStringName="LocalMySqlServer" writeExceptionsToEventLog="True" autogenerateschema="True" />
</providers>
</roleManager>
</system.web>
When run application I get this error:
Which step have I forgot? I cant use membership.
(Sorry for bad English)
If what you want is to use the Membership Provider that comes shipped with MVC4 Internet Templates of VS2010 or VS2012, with a database other than SQL Server Family, namely SQL Server, SQL Compact and Azure, I think that is not feasible, since MVC4 uses SimpleMembership provider, which is not Database Agnostic.
Have said that, that doesn't mean that you can't achieve what you want. I propose to use Microsoft ASP.NET Universal Providers Core Libraries. I recommend that you read this excellent Post
Now in this article the author, Scott Hanselman, mentions the following
Using these Universal "Default Profile Providers" means all you have to do is set the right connection string and your applications that use these services will work with SQL Server (plus Express), SQL Server Compact and SQL Azure with no code changes from you.
Hmmm and how about other SQL Standards?, well let's follow these steps,
1) Make sure to change the connection string first, and then with the Nuget Package Manager, install MySQL.Web, and MySQL.Data.
<connectionStrings>
<add name="MySQLConn" connectionString="Server=localhost;Database=dbname;Uid=dbuser;Pwd=dbpass;" />
</connectionStrings>
2) With the Nuget Package Manager find and install the "Microsoft ASP.NET Universal Providers Core Libraries" or run the following commands in the Package Manager Console,
Install-Package system.web.providers
Install-Package Microsoft.AspNet.Providers.Core
3) Go to Project Menu -> ASP.Net Configuration, you will see in the browser ASP Net Administration tool for your web project.
4) Then go to Provider Tab, Go to "Select a different provider for each feature (advanced)"
5) Make sure to select MySQL Membership Provider.
6) Then go Security Tab, and create some Roles and Users that you consider necessary for your project. If for some reason you get an error here, try changing your web config, and replace Membership, Role and Profile with this
7) Enjoy!
I hope this helps
PS: I'm very new on this too, so I'm open to suggestion about the best Membership Provider for MVC4 that targets others SQL Databases.