简体   繁体   中英

MySQL and Entity Framework issues

I am attempting to use EF6 to connect to a MySql DB. I've looked at NUMEROUS examples and they all look different. I see so many different ways and they are not like connecting to Oracle, which I have experience with.

public string GetWebinarList()
{
    string str = "";
    string connectionString = "server=127.0.0.1;port=3306;UserId=user;database=db;password=pwd;CharSet=utf8;Persist Security Info=True;";

    using (MySqlConnection connection = new MySqlConnection(connectionString))
    {
        connection.Open();

        using (webinarListDbContext context = new webinarListDbContext())
        {
            var list = context.WebinarLists.ToString();
            str = list;
        }
        connection.Close();
    }
    return str;
}

The above actually looks more like connecting through ADO.DB than EF6.

The Context definition:

[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class webinarListDbContext : DbContext
{
    public webinarListDbContext() : this("MonkeyFist") { }
    public webinarListDbContext(string connStringName) : base(connStringName) { }
    static webinarListDbContext()
    {
        // static constructors are guaranteed to only fire once per application.
        // I do this here instead of App_Start so I can avoid including EF
        // in my MVC project (I use UnitOfWork/Repository pattern instead)
        DbConfiguration.SetConfiguration(new MySql.Data.Entity.MySqlEFConfiguration());
    }
    public DbSet<WebinarList> WebinarLists { get; set; }
}

Web.Config:

<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, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
    <connectionStrings>
        <add name="MonkeyFist" providerName="MySql.Data.MySqlClient" connectionString="server=127.0.0.1;port=3306;UserId=user;database=db;password=pwd;CharSet=utf8;Persist Security Info=True;"/>
    </connectionStrings>
</system.data>
<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </providers>
</entityFramework>

But this is what I see when I inspect the context object:

在此处输入图片说明

What the heck is it using a SQLClient connection for vs. a MySQLClient? And Why would MonkeyFist be set as the Database? How would I connect EF6 to MySQL?

The reason you are seeing this behaviour is that you are not actually passing your connection object to your context. Instead you are passing the string "MonkeyFist", which the context assumes is the name of a db. Since it cannot find this db in your config file, it creates a local db with the same name.

See here: https://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext(v=vs.113).aspx

If the parameterless DbContext constructor is called from a derived context, then the name of the derived context is used to find a connection string in the app.config or web.config file. If no connection string is found, then the name is passed to the DefaultConnectionFactory registered on the Database class. The connection factory then uses the context name as the database name in a default connection string. (This default connection string points to .\\SQLEXPRESS on the local machine unless a different DefaultConnectionFactory is registered.) Instead of using the derived context name, the connection/database name can also be specified explicitly by passing the name to one of the DbContext constructors that takes a string.

Pass your connection object to your context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM