简体   繁体   English

如何使用实体框架访问MySQL数据库

[英]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. 背景:我已经创建了一个ASP.net MVC 4应用程序和一个mysql数据库。 I used code-first to generate the databases. 我使用代码优先生成数据库。 The Web App is going to be used as the UI for interacting with the data. 该Web App将用作与数据交互的UI。 However the data is created in a separate C# program. 但是,数据是在单独的C#程序中创建的。 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. 问题:我不知道如何在c#应用程序中使用实体框架连接数据库。

I have already create a new model for the C# app to use. 我已经为要使用的C#应用​​程序创建了一个新模型。 I have read that using shared .DLL isn't the best idea. 我读过使用共享的.DLL并不是最好的主意。

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 您需要使用连接器-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. 您需要将连接字符串添加到app.config文件中,然后安装Entity Framework Power Tools,然后使用它对数据库中的模型进行反向工程。

When creating the connection string make sure to set Persist Security Info=True 创建连接字符串时,请确保将Persist Security Info = True设置为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 我遇到的另一个问题是Visual Studios一直尝试使用连接器的6.6.5版本,而不是安装的版本6.7.4。

so I had to add this line to the app.config file as well 所以我也必须将此行添加到app.config文件中

<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为包含数据库连接器的程序集设置绝对路径? 带有实体框架4.1的MySQL Connector 6.4.3 - How do I set the absolute path for a assembly containing Database connector? MySQL Connector 6.4.3 with Entity Framework 4.1 如何使用实体框架6数据上下文创建数据库? - How do I create database using entity framework 6 datacontext? 如何使用Entity Framework为静态数据建立数据库? - How do I seed a database with static data using Entity Framework? 如何确保使用Entity Framework 5插入幂等数据库? - How do I ensure an idempotent database insert with Entity Framework 5? 如何通过实体框架更改数据库项的属性? - How do I change the properties of database item through Entity Framework? 使用实体框架,如何访问与每个办公室(另一个实体)相对应的雇员数量(一个实体)? - Using Entity Framework, how do I access the number of employees (one entity) corresponding to each office (another entity)? 如何使用Entity Framework 6访问Middleware中的数据库 - How to access the database in Middleware using Entity Framework 6 使用 Entity Framework Core 数据库优先方法如何将我的实体与基础设施层分开? - With Entity Framework Core database first approach how do I separate my entity from infrastructure layer? 如何更改数据库 - Entity Framework使用的架构(mysql数据库)? - How to change the database - Schema used by Entity Framework (mysql database)? 使用实体框架核心访问MySql - Access MySql with Entity Framework Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM