简体   繁体   中英

How do I access a MySQL database with Entity Framework

Background: I have already create a ASP.net MVC 4 app and a mysql database. I used code-first to generate the databases. The Web App is going to be used as the UI for interacting with the data. However the data is created in a separate C# program. Which gets test data from a serial device.

Problem: I can't figure out how to connect to the database with entity framework in my c# application.

I have already create a new model for the C# app to use. I have read that using shared .DLL isn't the best idea.

this is what I have so far.

    private ReportDBContext db = new ReportDBContext();

    private void saveReport()
    {
        string connStr = "server=RnD-Chopper;user=CWXDAQ;database=ReportDB;port=3306;password=QADXWC;";
        MySqlConnection conn = new MySqlConnection(connStr);
        try
        {

            db.Reports.Add();

            // Perform database operations
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        conn.Close();
        Console.WriteLine("Done.");




        unlockGUI();
    }

report model

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using MySql.Data.Entity;
using MySql.Data.MySqlClient;
using System.Linq;
using System.Web;

namespace CXW__DAQ___Record.Models
{
    public class Report
    {
        [Display(Name = "Test ID #")]
        public int ID { get; set; }

        [Required(ErrorMessage = "Test Name is Required")]
        [Display(Name = "Test Name")]
        public string Name { get; set; }

        [Display(Name = "Date Tested")]
        public DateTime TestDate { get; set; }

        [Display(Name="Test Done For")]
        public string TestFor { get; set; }

        public string Comment { get; set; }

        public enum enumForceUnits { lbs };
        public enum enumTimeUnits { mS };

        [Required(ErrorMessage = "Unit of Force (ForceUnit) is required")]
        public enumForceUnits forceUnit;

        [Required(ErrorMessage = "Unit of Time (TimeUnit) is required")]
        public enumTimeUnits timeUnit;

        public virtual ICollection<Reading> Readings { get; set; }

    }


    public class Reading
    {
        public int ID { get; set; }

        public virtual Report Report { get; set; }

        public long Time { get; set; }
        public double Force { get; set; }
    }


    public class ReportDBContext : DbContext
    {
        public DbSet<Report> Reports { get; set; }
        public DbSet<Reading> Readings { get; set; }
    }
}

You need to use the connector - http://dev.mysql.com/downloads/connector/net

Regenerate your model using the connector, too.

I figured it out myself. You need to add the connection string to the the app.config file and then install Entity Framework Power Tools then use that to reverse engineer the models from the database.

When creating the connection string make sure to set Persist Security Info=True

Also another problem I ran in to was that Visual Studios kept trying to use the 6.6.5 version of the connector instead of the version that was install which is 6.7.4

so I had to add this line to the app.config file as well

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.6.5.0" newVersion="6.7.4.0" />           
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

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