I'm trying to use entity framework, but I can't connect to my database.
Steps I have followed:
- Add ADO.NET entity data model and select Mysql, input the correct data, testing connection and then selecting the database table I wanted
- Visual Studio created a file named "DatabaseContext" and a file for each table selected.
DatabaseContext:
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);
}
}
}
Example of class generated automatically:
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; }
}
}
Tried to connect to the database in the main program
using (var db = new DatabaseContext()) { Console.Write(db.Database.Connection.State); //return always "Closed" Console.WriteLine(); }
4: Connection string created by visual studio
<connectionStrings>
<add name="Database1" connectionString="server=localhost;user id=root;password=asdasdasd;persistsecurityinfo=True;database=transcooptrips" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
The output of Console.Write(db.Database.Connection.State); is always "Closed".
What did I do wrong?



