2

I need to work with data in a couple of tables in an off site MySQL database we have limited access to and decided to use it as an opportunity to pick up some EFCF experience. No matter what I do i cannot get any data out of the MySQL database. Using MySQL workbench I can confirm the connection details are correct and can access the tables necessary.

Below is the current config edited a little to protect servers, etc.

Web Config

<connectionStrings>
    <clear />
    <add name="foo" connectionString="Server=111.222.333.444; Database=foo; Uid=foouser; Pwd=bar;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<system.data>
    <DbProviderFactories>
        <remove invariant="MySql.Data.MySqlClient" />
        <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
    </DbProviderFactories>
</system.data>

DbContext

public class DbContext : System.Data.Entity.DbContext
{
    public DbContext()
        : base("foo")
    {

    }

    public DbSet<Model.resource_request> resource_requests { get; set; }
}

Resource Requests

[Table("resource_requests")]
public class resource_request
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]  
    public Int64 id { get; set; }

    public string CustomerName { get; set; }
    public string ContactEmail { get; set; }
}

Quickly checking the context state using the immediate window I receive:

ctx.Database.Connection.Open()
Expression has been evaluated and has no value
ctx.Database.Connection
{MySql.Data.MySqlClient.MySqlConnection}
    [MySql.Data.MySqlClient.MySqlConnection]: {MySql.Data.MySqlClient.MySqlConnection}
    base {System.ComponentModel.Component}: {MySql.Data.MySqlClient.MySqlConnection}
    ConnectionString: "server=111.222.333.444;database=foo;User Id=foouser"
    ConnectionTimeout: 15
    Database: "foo"
    DataSource: "111.222.333.444"
    ServerVersion: "5.1.43-community"
    State: Open

However executing a query results in

ctx.resource_requests.Count();

Exception details

I'm sure I must be missing something obvious but what is it?

Many Thanks

Hawxby
  • 2,746
  • 21
  • 29
  • This was a while back so from memory I think it related to a bug in the datatype conversion of the adapter. In the end I think I downloaded the source and made the necessary tweaks. – Hawxby Dec 22 '11 at 17:56

3 Answers3

3

Have you tried to name your connection string DbContext ? It's name should match the class name.

Like this:

<add name="DbContext" connectionString="Server=111.222.333.444; Database=foo; Uid=foouser; Pwd=bar;" providerName="MySql.Data.MySqlClient" />

Haven't tried yet with MySql, but I had issues in the past with Sql Server when not naming properly the connection string.

Best regards!

Guilherme Melo
  • 494
  • 4
  • 8
  • Six hours down the toobs. Can somebody tell me where this is documented on the MySQL site? Thank you Guilherme Melo, you get my +1 – THBBFT Mar 24 '15 at 23:53
0

I found an answer here: MySQL with Entity Framework - what am I doing wrong?

Problem was with missing reference to MySql.Data.Entity in my project. After adding it, exception is gone.

Community
  • 1
  • 1
Katulus
  • 691
  • 1
  • 7
  • 12
0

Maybe this could help Entity Framework throws NullReferenceException with .NET 3.5

Another thing to be aware of is mysql lowercases column and table names so you should need to map Properties to field names when the model is defined.Custom Database Schema Mapping

chok68
  • 875
  • 9
  • 10