简体   繁体   English

MySQL和实体框架问题

[英]MySQL and Entity Framework issues

I am attempting to use EF6 to connect to a MySql DB. 我正在尝试使用EF6连接到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. 我看到了很多不同的方式,它们不像我所经历的那样连接到Oracle。

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. 上面的内容实际上看起来更像是通过ADO.DB而不是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: 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? 使用SQLClient连接与MySQLClient相比到底有什么用? And Why would MonkeyFist be set as the Database? 为何将MonkeyFist设置为数据库? How would I connect EF6 to MySQL? 我如何将EF6连接到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. 相反,您传递的是字符串“ MonkeyFist”,上下文假定该字符串是数据库的名称。 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 参见此处: https : //msdn.microsoft.com/zh-cn/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. 如果从派生上下文调用无参数DbContext构造函数,则派生上下文的名称用于在app.config或web.config文件中查找连接字符串。 If no connection string is found, then the name is passed to the DefaultConnectionFactory registered on the Database class. 如果找不到连接字符串,则该名称将传递到在Database类上注册的DefaultConnectionFactory。 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. (除非已注册其他DefaultConnectionFactory,否则此默认连接字符串指向本地计算机上的。\\ SQLEXPRESS。)也可以通过将名称传递给DbContext构造函数之一来显式指定连接/数据库名称,而不使用派生的上下文名称。需要一个字符串。

Pass your connection object to your context. 将连接对象传递给上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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