[英]Can't connect to mysql database using entity framework
我正在嘗試使用實體框架,但無法連接到我的數據庫。
我遵循的步驟:
數據庫上下文:
namespace TranscoopTrips.Database
{
using System.Data.Entity;
public partial class DatabaseContext : DbContext
{
public DatabaseContext()
: base("name=Database1")
{
}
public virtual DbSet<address> addresses { get; set; }
public virtual DbSet<driver> drivers { get; set; }
public virtual DbSet<journey> journeys { get; set; }
public virtual DbSet<vehicle> vehicles { get; set; }
public virtual DbSet<expeditionpoint> expeditionpoints { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<address>()
.Property(e => e.name)
.IsUnicode(false);
modelBuilder.Entity<address>()
.HasMany(e => e.expeditionpoints)
.WithOptional(e => e.address)
.HasForeignKey(e => e.fk_address)
.WillCascadeOnDelete();
modelBuilder.Entity<driver>()
.Property(e => e.name)
.IsUnicode(false);
modelBuilder.Entity<driver>()
.Property(e => e.surname)
.IsUnicode(false);
modelBuilder.Entity<driver>()
.HasMany(e => e.journeys)
.WithRequired(e => e.driver)
.HasForeignKey(e => e.fk_driver);
modelBuilder.Entity<journey>()
.Property(e => e.measureunit)
.IsUnicode(false);
modelBuilder.Entity<journey>()
.Property(e => e.customer)
.IsUnicode(false);
modelBuilder.Entity<journey>()
.HasMany(e => e.expeditionpoints)
.WithOptional(e => e.journey)
.HasForeignKey(e => e.fk_journey)
.WillCascadeOnDelete();
modelBuilder.Entity<vehicle>()
.Property(e => e.plate)
.IsUnicode(false);
modelBuilder.Entity<vehicle>()
.HasMany(e => e.journeys)
.WithRequired(e => e.vehicle)
.HasForeignKey(e => e.fk_vehicle);
}
}
}
自動生成的 class 示例:
namespace TranscoopTrips.Database
{
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
[Table("transcooptrips.driver")]
public partial class driver
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public driver()
{
journeys = new HashSet<journey>();
}
[Column(TypeName = "text")]
[Required]
[StringLength(65535)]
public string name { get; set; }
[Column(TypeName = "text")]
[Required]
[StringLength(65535)]
public string surname { get; set; }
public int id { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<journey> journeys { get; set; }
}
}
嘗試在主程序中連接數據庫
using (var db = new DatabaseContext()) { Console.Write(db.Database.Connection.State); //return always "Closed" Console.WriteLine(); }
4:Visual Studio 創建的連接字符串
<connectionStrings>
<add name="Database1" connectionString="server=localhost;user id=root;password=asdasdasd;persistsecurityinfo=True;database=transcooptrips" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
Console.Write(db.Database.Connection.State);
始終“關閉”。
我做錯了什么?
這是來自另一個論壇有人遇到同樣的問題,這是它的解決方案:
安裝 Nuget Package:
Install-Package EntityFramework Install-Package MySql.Data.Entity -Version 6.9.9 Install MySQL for Visual Studio 1.2.6 - https://dev.mysql.com/downloads/windows/visualstudio/
Web.Config 的變化
<EntityFramework>
至:
<EntityFramework codeConfigurationType = "MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
添加(**您的信息**):
<connectionStrings>
<add name="**YourContextName**" connectionString="server=**xxx.xxx.xxx.xxx**;port=3306;user id=**your user**;password=**your password**;database=**your database**" providerName="MySql.Data.MySqlClient" /></connectionStrings>
重新啟動 Visual Studio
我的設置: - Microsoft Visual Studio Community 2015 - Dot Net Framework 4.5.2 - Asp.Net MVC 5.2.3.0 - MySql Server 5.6
一個快速的建議:
檢查您的 sql 服務是否正在運行。
檢查數據庫名稱是否正確 - 您的實際數據庫名稱似乎是“transcooptrips”,並且您已將“name=Database1”傳遞給基礎 class。 定義此類常量的最佳方式是在 appsettings.json 中。
公共部分 class DatabaseContext: DbContext { public DatabaseContext(): base("name=Database1") { }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.