开发者

Entity Framework Code First + MySQL... NullReferenceException

开发者 https://www.devze.com 2023-03-30 22:51 出处:网络
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 ca

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 tab开发者_StackOverflow中文版les 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();

Entity Framework Code First + MySQL... NullReferenceException

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

Many Thanks


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!


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消