【问题标题】:How do I access a MySQL database with Entity Framework如何使用实体框架访问 MySQL 数据库
【发布时间】:2013-08-01 18:10:57
【问题描述】:

背景:我已经创建了一个 ASP.net MVC 4 应用程序和一个 mysql 数据库。我使用代码优先来生成数据库。 Web App 将用作与数据交互的 UI。但是,数据是在单独的 C# 程序中创建的。它从串行设备获取测试数据。

问题: 我不知道如何在我的 c# 应用程序中使用实体框架连接到数据库。

我已经创建了一个新模型供 C# 应用程序使用。我已经读过使用共享的 .DLL 不是最好的主意。

这是我目前所拥有的。

    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();
    }

报表模型

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

【问题讨论】:

  • 您是如何生成模型的?来自什么数据库?
  • 我在 ASP.net MVC 4 中首先使用了代码(该应用程序运行良好),然后我只是复制了 .cs 文件并删除了我不需要的模型/表。我找不到关于这样做的教程,所以我只是坐在裤子的座位上飞行。

标签: c# mysql entity-framework c#-4.0 ef-code-first


【解决方案1】:

您需要使用连接器 - http://dev.mysql.com/downloads/connector/net

也使用连接器重新生成模型。

【讨论】:

  • 我就是 MySqlConnection 的一部分。
  • 实体框架管理连接。您可以通过作为模型一部分生成的 DatabaseContext 类访问数据库。
  • 所以我只需要在我的 web.config 中添加一个连接字符串吗?然后它处理其余的?就像 ASP.net 一样?
  • 如何从现有数据库生成模型?
【解决方案2】:

我自己想通了。您需要将连接字符串添加到 app.config 文件中,然后安装 Entity Framework Power Tools,然后使用它从数据库中对模型进行逆向工程。

创建连接字符串时,请确保设置 Persist Security Info=True

我遇到的另一个问题是 Visual Studios 一直尝试使用 6.6.5 版本的连接器,而不是安装的 6.7.4 版本

所以我也必须将此行添加到 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 2011-11-05
    相关资源
    最近更新 更多