如何使用 MySQL 设置 ASP.NET MVC 2?
是否可以设置 ASP.NET MVC 2 以使用 MySQL 数据库?
Is it possible to setup ASP.NET MVC 2 to work with a MySQL database?
推荐答案
我假设您拥有 Visual Studio Professional 2008,可以访问 MySQL 服务器的实例,并且具有中到高级的开发经验.这可能适用于 VS2008 网络版,但完全不确定.
I'm assuming that you have Visual Studio Professional 2008, have access to an instance of MySQL server, and have moderate to advanced development experience. This MAY work with VS2008 Web edition, but not at all sure.
- 如果您还没有安装 MySQL Connector for .NET (6.2.2.0 在撰写本文时)
- 可选:安装 MySQL GUI 工具
- 如果尚未安装,请安装 MVC 2 RTM,或者更好的是,使用 Microsoft 的 Web 平台安装程序.(更新:MVC 2 已经发布了一段时间)
- 创建一个空的 MySQL 数据库.如果您不想使用 MySQL 根用户帐户(不安全)访问您的应用程序,请创建一个用户帐户并分配适当的权限(不在本文讨论范围内).
- 在 Visual Studio 中创建一个新的 MVC 2 应用程序
- 在 MVC 2 应用程序中,引用 MySql.Web.dll.它将位于您的 GAC 中,或者位于 MySQL 连接器安装程序放置它的文件夹中.
修改 web.config 的连接字符串部分:
- If you haven't, install MySQL Connector for .NET (6.2.2.0 at the time of this write-up)
- Optional: install MySQL GUI Tools
- If you haven't, install MVC 2 RTM, or better yet, use Microsoft's Web Platform Installer. (UPDATE: MVC 2 has now been released for quite some time)
- Create an empty MySQL database. If you don't want to access your application with the MySQL root user account (insecure), create a user account and assign the appropriate privileges (outside the scope of this write-up).
- Create a new MVC 2 application in Visual Studio
- In the MVC 2 app, reference MySql.Web.dll. It will either be in your GAC, or in the folder that the MySQL Connector installer put it.
Modify the connection strings portion of your web.config:
<connectionStrings>
<remove name="LocalMySqlServer"/>
<add name="MySqlMembershipConnection"
connectionString="Data Source=[MySql server host name];
userid=[user];
password=[password];
database=[database name];"
providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
8.
修改 web.config 的成员资格部分:
Modify the membership portion of your web.config:
<membership defaultProvider="MySqlMembershipProvider">
<providers>
<clear/>
<add name="MySqlMembershipProvider"
type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web,
Version=6.2.2.0, Culture=neutral,
PublicKeyToken=c5687fc88969c44d"
connectionStringName="MySqlMembershipConnection"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="true"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
applicationName="/"
autogenerateschema="true"/>
</providers>
</membership>
9.
修改 web.config 的角色管理器部分:
Modify the role manager portion of your web.config:
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<providers>
<clear />
<add connectionStringName="MySqlMembershipConnection"
applicationName="/"
name="MySqlRoleProvider"
type="MySql.Web.Security.MySQLRoleProvider, MySql.Web,
Version=6.2.2.0, Culture=neutral,
PublicKeyToken=c5687fc88969c44d"
autogenerateschema="true"/>
</providers>
</roleManager>
10.
修改 web.config 的配置文件部分:
Modify the profile portion of your web.config:
<profile>
<providers>
<clear/>
<add type="MySql.Web.Security.MySQLProfileProvider, MySql.Web,
Version=6.2.2.0, Culture=neutral,
PublicKeyToken=c5687fc88969c44d"
name="MySqlProfileProvider"
applicationName="/"
connectionStringName="MySqlMembershipConnection"
autogenerateschema="true"/>
</providers>
</profile>
此时,您应该能够运行应用程序并在浏览器中显示默认的 ASP.NET MVC 2 主页.但是,最好先运行 ASP.NET Web 配置工具(在 Visual Studio 顶部菜单中:项目 -> ASP.NET 配置).工具启动后,检查每个选项卡;没有错误 = 一切顺利.
At this point, you ought to be able to run the app and have the default ASP.NET MVC 2 home page come up in your browser. However, it may be a better idea to first run the ASP.NET Web configuration Tool (in Visual Studio top menus: Project -> ASP.NET Configuration). Once the tool launches, check out each of the tabs; no errors = all good.
Nathan Bridgewater 的博客 对实现这一目标至关重要.荣誉,内森.寻找页面中间的配置工具".
The configuration tool at Nathan Bridgewater's blog was essential to getting this working. Kudos, Nathan. Look for the "Configuration Tool" heading half way down the page.
我在这里发布的 MySql.web.dll 上的公钥令牌应该不会很快改变.但是,如果您怀疑通过复制和粘贴或其他方式产生了错误的令牌字符串,只需使用 Visual Studio 命令行运行:sn -T [Path\to\your.dll]"以获得正确的公钥令牌.
The public key token on the MySql.web.dll that I've posted here ought not change any time soon. But in case you suspect a bad token string from copying and pasting or whatever, just use the Visual Studio command line to run: "sn -T [Path\to\your.dll]" in order to get the correct public key token.
你知道了,ASP.NET MVC 2 在 MySQL 上运行.干杯!
There you have it, ASP.NET MVC 2 running over MySQL. Cheers!
相关文章