繁体   English   中英

MySQL和实体框架问题

[英]MySQL and Entity Framework issues

我正在尝试使用EF6连接到MySql DB。 我看过许多示例,它们看起来都不同。 我看到了很多不同的方式,它们不像我所经历的那样连接到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;
}

上面的内容实际上看起来更像是通过ADO.DB而不是EF6连接。

上下文定义:

[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>

但这是检查上下文对象时看到的:

在此处输入图片说明

使用SQLClient连接与MySQLClient相比到底有什么用? 为何将MonkeyFist设置为数据库? 我如何将EF6连接到MySQL?

您看到此行为的原因是您实际上没有将连接对象传递给上下文。 相反,您传递的是字符串“ MonkeyFist”,上下文假定该字符串是数据库的名称。 由于它无法在您的配置文件中找到该数据库,因此它将创建一个具有相同名称的本地数据库。

参见此处: https : //msdn.microsoft.com/zh-cn/library/system.data.entity.dbcontext(v=vs.113).aspx

如果从派生上下文调用无参数DbContext构造函数,则派生上下文的名称用于在app.config或web.config文件中查找连接字符串。 如果找不到连接字符串,则该名称将传递到在Database类上注册的DefaultConnectionFactory。 然后,连接工厂将上下文名称用作默认连接字符串中的数据库名称。 (除非已注册其他DefaultConnectionFactory,否则此默认连接字符串指向本地计算机上的。\\ SQLEXPRESS。)也可以通过将名称传递给DbContext构造函数之一来显式指定连接/数据库名称,而不使用派生的上下文名称。需要一个字符串。

将连接对象传递给上下文。

暂无
暂无

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

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