简体   繁体   English

将MySql与ASP.NET MVC一起使用(Entity Framework 6)

[英]Using MySql with ASP.NET MVC (Entity Framework 6)

My database originally built with MSSQL but recently I've migrated it over to MySql . 我的数据库最初是用MSSQL构建的,但是最近我将其迁移到MySql However, now I need to connect it to my MVC project (existing). 但是,现在我需要将其连接到我的MVC项目(现有)。 I tried doing it by utilizing the Entity Framework wizard but get the following error message. 我尝试通过使用Entity Framework向导来执行此操作,但收到以下错误消息。

错误信息

I read along and found that I need some classes that are required for MySql to work with Visual Studio and added the following NuGet packages. 我仔细阅读,发现我需要MySql与Visual Studio一起使用所需的一些类,并添加了以下NuGet包。 I even installed the MySql Microsoft package (with the connector + MySql for Visual Studio). 我什至安装了MySql Microsoft程序包(带有用于Visual Studio的连接器+ MySql)。

NuGet软件包

Even with all the packages installed I still get this error message. 即使安装了所有软件包,我仍然会收到此错误消息。 How can I connect my existing MVC project to a MySql database? 如何将现有的MVC项目连接到MySql数据库?

First install three Package Manager Console one by one (Tools->NutGet Package Manger->Package Manger Console) 首先一个接一个地安装三个Package Manager控制台(“工具”->“ NutGet软件包管理器”->“软件包管理器控制台”)

PM> Install-Package EntityFramework
PM> Update-Package EntityFramework
PM> Install-Package MySql.Data.Entity

Web.config 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>

Models DbContext 模型DbContext

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; }
    }
}

Customer Model Class 客户模型类别

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; }
    }
}

Controller Class 控制器类

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());
        }
}
}

View Page 查看页面

@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>

Maybe you have already found yourself a solution, but if you didn't, try: Tools -> NuGet Package Manager -> Package Manager Console, and update-package entityframework I've been through this myself. 也许您已经找到了自己的解决方案,但如果没有找到解决方案,请尝试:工具-> NuGet软件包管理器->软件包管理器控制台,以及更新软件包实体框架。 ;-) ;-)

Check what version of MySql.Data.Entity you have installed. 检查已安装的MySql.Data.Entity版本。 I have 6.9.5, and it worked for me. 我有6.9.5,它为我工作。

To update, run Update-Package MySql.Data.Entity in the Package Manager Console 若要更新,请在程序包管理器控制台中运行Update-Package MySql.Data.Entity

You may have to add the -Pre for the pre-release version. 您可能必须为预发行版本添加-Pre

Try to change you web.config, add this 尝试更改您的web.config,添加它

<entityFramework>
    <defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
    </providers>
  </entityFramework>

and this to 而这个

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM