[英]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.