Giter Site home page Giter Site logo

mysql-entityframework-code-first's Introduction

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

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

<connectionStrings>
    <add name="SellRightDb" providerName="MySql.Data.MySqlClient"connectionString="server=localhost;userid=[your userid];password=[your password]database=sellright;persistsecurityinfo=True"/>
  </connectionStrings>

Entity Framework Configuration
Configuring MySQL to use Entity Framework add the block below to your Web.config or App.config

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
    <providers>
      <provider invariantName="MySql.Data.MySqlClient"
          type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>
      <provider invariantName="System.Data.SqlClient"
          type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
    </providers>
  </entityFramework>

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 DbSet<Product>Products { 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 
  1. 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]

mysql-entityframework-code-first's People

Contributors

frankodoom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

mysql-entityframework-code-first's Issues

Unrecognized element 'providers'

When i try to code, i got errors like this.
System.TypeInitializationException: 'The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.' Inner Exception ConfigurationErrorsException: Unrecognized element 'providers'.
here my app.config

<providers>        
        <provider invariantName="MySql.Data.MySqlClient"
            type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>            
        <provider invariantName="System.Data.SqlClient"
            type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
      </providers>

please advice.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.