所以我刚刚开始使用VS2012,想要开始一个带EF5的ASP.NET MVC 4应用程序。
我的主机没有MSSQL,所以我必须使用MySQL。
我该如何告诉我的应用程序它应该使用MySQL?(我想要使用devart MySQL连接器或者来自mysql.com的连接器)
我的主机没有MSSQL,所以我必须使用MySQL。
我该如何告诉我的应用程序它应该使用MySQL?(我想要使用devart MySQL连接器或者来自mysql.com的连接器)
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient"/>
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient"
description=".Net Framework Data Provider for MySQL"
type="MySql.Data.MySqlClient.MySqlClientFactory,MySql.Data" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="ConnectionStringName"
connectionString="Datasource=hostname;Database=schema_name;uid=username;pwd=Pa$$w0rd;"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
如果您正在使用NuGet(6.5.4)中的MySql连接器,则需要自定义初始化器。可在http://brice-lambson.blogspot.se/2012/05/using-entity-framework-code-first-with.html或http://www.nsilverbullet.net/2012/11/07/6-steps-to-get-entity-framework-5-working-with-mysql-5-5/获取代码。
然后将其添加到配置中。
<configSections>
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,
EntityFramework, Version=5.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" />
</configSections>
<entityFramework>
<contexts>
<context type="Namespace.YourContextName, AssemblyName">
<databaseInitializer
type="Namespace.YourChosenInitializer, AssemblyName">
</databaseInitializer>
</context>
</contexts>
<defaultConnectionFactory
type="MySql.Data.MySqlClient.MySqlClientFactory,MySql.Data" />
</entityFramework>
<membership defaultProvider="MySqlMembershipProvider">
<providers>
<clear />
<add name="MySqlMembershipProvider"
type="MySql.Web.Security.MySQLMembershipProvider,
MySql.Web, Version=6.5.4.0, PublicKeyToken=c5687fc88969c44d"
autogenerateschema="true"
connectionStringName="*NAME_OF_YOUR_CONN_STRING*"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="false"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
passwordStrengthRegularExpression=""
applicationName="/" />
</providers>
</membership>
让AccountController和Views工作起来:
@Html.Partial(“_LogOnPartial”)
替换@Html.Partial(“_LoginPartial”)
<add name="ConnectionString" providerName="MySql.Data.MySqlClient" connectionString="Data Source=127.0.0.1; port=3306; Initial Catalog=DbName; uid=root; pwd=*Password*;" />
PM> Install-Package EntityFramework
PM> Update-Package EntityFramework
PM> Install-Package MySql.Data.Entity
Web.config
<connectionStrings>
<add name="DefaultConnection"
providerName="MySql.Data.MySqlClient"
connectionString="Data Source=localhost;port=3306;Initial Catalog=api_db;User Id=root;password=''"/>
</connectionStrings>
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace LiteRemit.Models
{
[Table("customers")]
public class CustomerModel
{
[Key]
public int CustomerId { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace LiteRemit.Models
{
public class MySqlCon : DbContext
{
//MySql Database connection String
public MySqlCon() : base(nameOrConnectionString: "DefaultConnection") { }
public virtual DbSet<CustomerModel> Customers { get; set; }
}
}
using LiteRemit.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace LiteRemit.Controllers
{
public class HomeController : Controller
{
MySqlCon _con;
public HomeController()
{
_con = new MySqlCon();
}
public ActionResult Index()
{
return View(_con.Customers.ToList());
}
}
}
@using LiteRemit.Models
@model IEnumerable<CustomerModel>
<table border="1">
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.CustomerId)</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>@Html.DisplayFor(modelItem => item.Country)</td>
</tr>
}
</table>