简体   繁体   中英

Using Visual Studio 2013 with Entity Framework 6 how to use code first with a mySql database

I've been working on a WPF application. I'm trying to include a MySql database. Outside of using MySql for Visual Studio I've not seen a good tutorial for using Entity Framework 6 with MySql database's. Anyone have a good tutorial for WPF that I can use or what I need next. Most of what I have seen doesn't have code first.

So far what I have is a connection to a empty mysql db in the Server Explorer. Secondly I've got my packages.config file :

<packages>
  <package id="CommonServiceLocator" version="1.2" targetFramework="net45" />
  <package id="EntityFramework" version="6.1.1" targetFramework="net45" />
  <package id="ModernUI.WPF" version="1.0.5" targetFramework="net45" />
  <package id="MvvmLight" version="4.2.30.0" targetFramework="net45" />
  <package id="MvvmLightLibs" version="4.3.31.1" targetFramework="net45" />
  <package id="MySql.Data" version="6.9.2-rc1" targetFramework="net45" />
  <package id="MySql.Data.Entity" version="6.9.2-rc1" targetFramework="net45" />
</packages>

Then I've got my App.config file... Haven't done anything with this as I am not sure what to do :

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Practices.ServiceLocation" p ublicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.2.0.0" newVersion="1.2.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
<connectionStrings><add name="Model1" connectionString="data source=(LocalDb)\v11.0;initial catalog=_12_RSE_002.Model1;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /></connectionStrings></configuration>  

First you need to add a reference to Mysql.Data.Entity to your project.

As a first tutorial i would recommend this one.

In this tutorial you don't need to specify a connection string because visual studio will do it for you when you create ADO.NET Entity Data Model.

EDIT :

In the connectionStrings section: you need to add the connectionString to your MYSQL database, you can replace the one called Model1 or add another one like this :

<add name="Model1" connectionString="server=127.0.0.1;Uid=root;Database=mysqlentityframework" providerName="MySql.Data.MySqlClient" />

Also in the Providers section you need to add a provider for Mysql.Data.MysqlClient :

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

this provider is used by the connection string to specify which database to use, you can delet the System.Data.SqlClient, you have nothing to do with it now, so your final app.config file should look something like this:

  <?xml version="1.0" encoding="utf-8"?>
 <configuration>
<configSections>
  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
  <providers>
    <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"></provider>   
</providers>
</entityFramework>
<connectionStrings>
  <add name="Model1" connectionString="server=127.0.0.1;Uid=root;Database=mysqlentityframework" providerName="MySql.Data.MySqlClient" />
</connectionStrings>

this is working for me, all you need to do is add an ADO.NET entity data Model and create your Entities.

EDIT2: this is the code first example:Model.cs FILE

namespace TestsAndTests
{
using System;
using System.Data.Entity;
using System.Linq;

public class Model1 : DbContext
{
    public Model1()
        : base("name=Model1")
    {
    }

    public DbSet<students> Students { get; set; } 

}

public class students
{
    [Key]
    public int StudentI { get; set; }
    public string StudentName { get; set; }
}
}

Program.cs Main function:

students student = new students
        {
            StudentI =  3,
            StudentName = "test1"
        };
        Model1 model = new Model1();
        model.Students.Add(student);
        model.SaveChanges();
        Console.WriteLine("object Added");

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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