2013-08-01 58 views
1

背景:我已經創建了一個ASP.net MVC 4應用程序和一個mysql數據庫。我使用代碼優先生成數據庫。 Web應用程序將用作與數據交互的UI。但是,數據是在單獨的C#程序中創建的。從串行設備獲取測試數據。如何使用實體框架訪問MySQL數據庫

問題: 我不知道如何連接到與我的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; } 
    } 
} 
+0

你是如何生成模型?什麼數據庫關閉? –

+0

我使用的代碼首先在ASP.net MVC 4,(該應用程序工作正常),然後我剛剛複製.cs文件,並刪除我不需要的模型/表。我找不到這樣做的教程,所以我只是坐在我的褲子旁邊。 – TheColonel26

回答

0

我想出了自己。您需要將連接字符串添加到app.config文件,然後安裝Entity Framework Power Tools,然後使用該字符串對數據庫中的模型進行逆向工程。

當創建一個連接字符串請務必將堅持安全信息=真

也是另一個問題,我跑了到是視覺工作室一直試圖使用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> 
1

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

使用連接器重新生成模型了。

+0

我就是MySqlConnection的一部分。 – TheColonel26

+0

實體框架管理連接。您可以通過作爲模型的一部分生成的DatabaseContext類訪問數據庫。 –

+0

那麼,我只需要添加一個連接字符串到我的web.config?然後它處理剩下的事情?就像ASP.net一樣? – TheColonel26

相關問題