我正在尝试使用实体框架,但无法连接到数据库。
我遵循的步骤:
DatabaseContext:
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.fireChannelActive();
}
自动生成的类的示例:
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);
}
}
}
试图连接到主程序中的数据库
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; }
}
}
4:Visual Studio创建的连接字符串
using (var db = new DatabaseContext())
{
Console.Write(db.Database.Connection.State); //return always "Closed"
Console.WriteLine();
}
<connectionStrings>
<add name="Database1" connectionString="server=localhost;user id=root;password=asdasdasd;persistsecurityinfo=True;database=transcooptrips" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
的输出始终为“关闭”。
我做错了什么?
答案 0 :(得分:2)
这是另一个人遇到的同样问题的论坛,这是它的解决方案:
安装Nuget软件包:
安装包EntityFramework 安装软件包MySql.Data.Entity-版本6.9.9 安装适用于Visual Studio 1.2.6的MySQL-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社区2015 -点网框架4.5.2 -Asp.Net MVC 5.2.3.0 -MySql Server 5.6
答案 1 :(得分:0)
快速建议:
检查您的sql服务是否正在运行。
检查数据库名称是否正确-看来您的实际数据库名称是“ transcooptrips”,并且您已将“ name = Database1”传递给基类。定义此类常量的最优选方法是在appsettings.json中。
公共局部类DatabaseContext:DbContext { 公共DatabaseContext() :base(“名称=数据库1”) { }