Settings at 11/2018
.Net Framework 4.5.2 using project properties
Mysql.Data.Entity using NuGet(6.10.8)
Setup Web.config
<!--Added-->
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.2.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="ferredb" connectionString="server=127.0.0.1;Port=3306;Database=ferre;Uid=root;Pwd=Microsoft;" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
<!--Added-->
<entityFramework>
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.10.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
</provider>
</providers>
</entityFramework>
<!--Added-->
<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, Version=6.10.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
</DbProviderFactories>
</system.data>
Fix version of entry System.Web.Mvc.MvcWebRazorHostFactory at views/Web.config
Classes
//Ado
using System;
using System.Data.Entity;
using MySQLEntity.Models;
namespace MySQLEntity.Entity
{
public class MyDb : DbContext
{
public MyDb() : base("ferredb")
//public MyDb() : base(nameOrConnectionString: "ferredb")
{
}
public DbSet<Usuario> Usuarios { get; set; }
}
}
//Entity
using System;
//Added
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace MySQLEntity.Models
{
[Table("Users")]
public class Usuario
{
[Key]
public string usuario { get; set; }
public string apenom { get; set; }
}
}
Controller Snippet code
public ActionResult Index()
{
MyDb db = new MyDb();
var Model=db.Usuarios.ToList();
return View(Model);
}
View Snippet code
@foreach (var item in Model)
{
<div> @item.usuario @item.apenom
</div>
}
Action results
References:
[1] MySQL EntityFramework web.config
https://iyalovoi.wordpress.com/2015/04/06/entity-framework-with-mysql-on-mac-os/
[2] Action results
https://www.c-sharpcorner.com/article/different-types-of-action-results-in-asp-net-mvc/
[3] ViewBag better than DataView
http://www.tutorialsteacher.com/mvc/viewbag-in-asp.net-mvc