Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用MySql和ASP.NET MVC(实体框架6)_C#_Mysql_Asp.net_Entity Framework_Visual Studio - Fatal编程技术网

C# 使用MySql和ASP.NET MVC(实体框架6)

C# 使用MySql和ASP.NET MVC(实体框架6),c#,mysql,asp.net,entity-framework,visual-studio,C#,Mysql,Asp.net,Entity Framework,Visual Studio,我的数据库最初是用MSSQL构建的,但最近我把它迁移到了MySql。但是,现在我需要将它连接到我的MVC项目(现有)。我尝试使用实体框架向导执行此操作,但收到以下错误消息 我一直在阅读,发现我需要一些类,这些类是MySql与visualstudio一起工作所必需的,并添加了以下NuGet包。我甚至安装了MySqlMicrosoft软件包(带有用于visualstudio的connector+MySql) 即使安装了所有的软件包,我仍然会收到这个错误消息。如何将现有的MVC项目连接到MySql

我的数据库最初是用
MSSQL
构建的,但最近我把它迁移到了
MySql
。但是,现在我需要将它连接到我的MVC项目(现有)。我尝试使用
实体框架
向导执行此操作,但收到以下错误消息

我一直在阅读,发现我需要一些类,这些类是
MySql
与visualstudio一起工作所必需的,并添加了以下NuGet包。我甚至安装了
MySql
Microsoft软件包(带有用于visualstudio的connector+MySql)


即使安装了所有的软件包,我仍然会收到这个错误消息。如何将现有的MVC项目连接到
MySql
数据库?

也许您已经找到了解决方案,但如果没有,请尝试: 工具->NuGet包管理器->包管理器控制台,并更新包entityframework
我自己也经历过这一切

检查您安装的MySql.Data.Entity的版本。我有6.9.5,它对我很有效

要更新,请在Package Manager控制台中运行更新包MySql.Data.Entity


您可能需要为预发布版本添加
-Pre

尝试更改您的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>

这是给

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

首先逐个安装三个Package Manager控制台(工具->NutGet 软件包管理器->软件包管理器控制台)

Web.config

控制器类

查看页面

@使用LiteRemit.Models
@模型IEnumerable
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.CustomerId)
@DisplayFor(modelItem=>item.Name)
@DisplayFor(modelItem=>item.Country)
}

我不确定这是否回答了特定的问题,但这肯定回答了我的google查询:D谢谢。即使在安装了软件包并将defaultconnection添加到mysql之后。在添加新项目时,我无法在EF Framework窗口中获取mysql提供程序。Onlu Sql server提供程序列表
PM> Install-Package EntityFramework
PM> Update-Package EntityFramework
PM> Install-Package MySql.Data.Entity
 <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.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 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 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>