NullReferenceException: Entity Framework Code First + MySQL

asp.net-4.0 c# entity-framework mysql

Question

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

1
2
8/25/2011 3:40:05 PM

Popular Answer

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!

3
12/22/2011 11:43:40 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow