Entity Framework Code First + MySQL… NullReferenceException

本小妞迷上赌 提交于 2019-12-08 02:11:45

问题


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();

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

Many Thanks


回答1:


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!




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/7192295/entity-framework-code-first-mysql-nullreferenceexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!