https://github.com/frankodoom/mysql-entityframework-code-first
:satisfied: Setting up MySQL .NET Connector using Entity Framework Code First
https://github.com/frankodoom/mysql-entityframework-code-first
entityframework-codefirst mysql mysql-server
Last synced: 4 months ago
JSON representation
:satisfied: Setting up MySQL .NET Connector using Entity Framework Code First
- Host: GitHub
- URL: https://github.com/frankodoom/mysql-entityframework-code-first
- Owner: frankodoom
- Created: 2017-02-04T10:00:03.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-12-03T16:28:58.000Z (about 8 years ago)
- Last Synced: 2025-04-01T07:02:11.464Z (9 months ago)
- Topics: entityframework-codefirst, mysql, mysql-server
- Language: C#
- Homepage:
- Size: 18.6 KB
- Stars: 16
- Watchers: 4
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# MySQL-EntityFramework-Code-First

MySQL is a Database Management System from Oracle that currently supports Entity Framework through the MySQL ADO.NET Connector a fully-managed ADO.NET driver for MySQL.. I will go through steps in setting up MySQL with Entity framework 6.1.+ Using Visual Studio.
#### Note:
This Tutorial also works with MariaDB.
**PREREQUISITES**
- Visual Studio
Download Free Community Version Here (https://www.visualstudio.com/vs/community/)
- MySQL Server
Download Here (https://dev.mysql.com/downloads/installer/)
- ADO.NET Driver for MySQL (Connector/NET)
Download Here (https://dev.mysql.com/downloads/connector/net/)
- Microsoft Entity Framework 6.1.+
**Adding Entity framework**
Download Entity Framework From Nugget in Visual Studio,this will automatically reference all needed Microsoft Entity Framework Assemblies in your project
-OR
Run this command in Package Management Console to download Entity Framework
` Pm> Install-Package EntityFramework`
**Reference MySQL Assemblies** (NB.These Assemblies are Available after installing the MYSQL Connector for .Net you can also get them on Nuget)
- MySQL.Data
- MySQL.Data.Entity
**Setting Up The Connection String in your `Web.Config` or `App.Config`**
Set connection string to your existing MySql Database
```
```
**Entity Framework Configuration**
Configuring MySQL to use Entity Framework add the block below to your `Web.config` or `App.config`
```
```
Create A Simple Model Class
```
class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public double Price { get; set; }
public int Quantity { get; set; }
}
```
Creating Your DBContext Class
Import Namespaces
```
using System.Data.Entity;
using MySql.Data.Entity;
```
Configuring your Db Contex Class
```
// Code-Based Configuration and Dependency resolution
[DbConfigurationType(typeof(MySqlEFConfiguration))]
class SellRightContext :DbContext
{
//Add your Dbsets here
public DbSetProducts { get; set; }
public SellRightContext()
//Reference the name of your connection string
: base("SellRightDb")
{
}
}`
```
NB.
Enable Code First Migration
```
PM> Enable-Migrations
```
Add Code First Migration
```
PM> Add-Migration [Give Your Migration Settings A name]
```
Update Database (Specify Verbose to see the SQL Querry that is being executed)
```
PM> Update-Database -Verbose
```
You should see your Product Table Created in Your MySql Server
#### Note:
If you get a **System.TypeLoadException** when adding a migration, you should try to install a older version of the MySQL packages.
To do so, follow these steps:
1. Open the Package Manager Console: **Tools > NuGet Package Manager > Package Manager Console**
2. Uninstall the current Packages:
```
PM> Uninstall-Package MySQL.Data
PM> Uninstall-Package MySQL.Data.Entity
```
3. Install the older packages:
```
PM> Install-Package MySQL.Data -Version [type in the tabulator and you'll see the available versions] `
PM> Install-Package MySQL.Data.Entity -Version [select the same version]
```