如何在ASP.NET 5中使用Entity Framework 6和MySQL?

时间:2022-01-11 14:42:17

I have an existing site that uses ASP.NET MVC 4, Entity Framework 6 and MySQL. I'm trying to upgrade it to ASP.NET 5, but want to continue using Entity Framework 6 as Entity Framework is missing some features and does not yet support MySQL. How do I use EF6 in ASP.NET 5?

我有一个使用ASP.NET MVC 4,Entity Framework 6和MySQL的现有站点。我正在尝试将其升级到ASP.NET 5,但是想要继续使用Entity Framework 6,因为Entity Framework缺少某些功能而且还不支持MySQL。如何在ASP.NET 5中使用EF6?

1 个解决方案

#1


2  

Since Web.config is no longer used with ASP.NET 5, you need to use code-based configuration to configure it instead. To do so, create a new class that inherits from DbConfiguration:

由于Web5config不再与ASP.NET 5一起使用,因此您需要使用基于代码的配置来配置它。为此,请创建一个继承自DbConfiguration的新类:

public class MyDbConfiguration : DbConfiguration
{
    public MyDbConfiguration()
    {
        // Register ADO.NET provider
        var dataSet = (DataSet)ConfigurationManager.GetSection("system.data");
        dataSet.Tables[0].Rows.Add(
            "MySQL Data Provider",
            ".Net Framework Data Provider for MySQL",
            "MySql.Data.MySqlClient",
            typeof(MySqlClientFactory).AssemblyQualifiedName
        );

        // Register Entity Framework provider
        SetProviderServices("MySql.Data.MySqlClient", new MySqlProviderServices());
        SetDefaultConnectionFactory(new MySqlConnectionFactory());
    }
}

The first part of the configuration is a hack to register the ADO.NET provider at runtime, by dynamically adding a new configuration entry to the system.data section. This is very hacky, but does appear to work correctly.

配置的第一部分是通过动态地向system.data部分添加新的配置条目来在运行时注册ADO.NET提供程序的hack。这非常hacky,但似乎确实正常工作。

Add the connection string to config.json rather than Web.config:

将连接字符串添加到config.json而不是Web.config:

{
  "Data": {
    "DefaultConnection": {
      "ConnectionString": "Server=localhost; Database=test; Uid=test; Pwd=password;"
    }
  }
}

Modify the DbContext to use the correct configuration and connection string:

修改DbContext以使用正确的配置和连接字符串:

[DbConfigurationType(typeof(MyDbConfiguration))]
public class MyContext : DbContext
{
    public MyContext(IConfiguration config)
      : base(config["Data:DefaultConnection:ConnectionString"])
      {
      }
      // ...
}

Register MyContext in the dependency injection container in Startup.cs:

在Startup.cs中的依赖注入容器中注册MyContext:

public void ConfigureServices(IServiceCollection services)
{
    // ...
    services.AddScoped<MyContext>();
}

Then you can just use constructor injection to get MyContext into your controllers.

然后你可以使用构造函数注入将MyContext导入控制器。

More details in my blog post at http://dan.cx/2015/08/entity-framework-6-mysql-aspnet, and a sample project at https://github.com/Daniel15/EFExample

有关详细信息,请访问我的博客文章http://dan.cx/2015/08/entity-framework-6-mysql-aspnet,以及https://github.com/Daniel15/EFExample上的示例项目

#1


2  

Since Web.config is no longer used with ASP.NET 5, you need to use code-based configuration to configure it instead. To do so, create a new class that inherits from DbConfiguration:

由于Web5config不再与ASP.NET 5一起使用,因此您需要使用基于代码的配置来配置它。为此,请创建一个继承自DbConfiguration的新类:

public class MyDbConfiguration : DbConfiguration
{
    public MyDbConfiguration()
    {
        // Register ADO.NET provider
        var dataSet = (DataSet)ConfigurationManager.GetSection("system.data");
        dataSet.Tables[0].Rows.Add(
            "MySQL Data Provider",
            ".Net Framework Data Provider for MySQL",
            "MySql.Data.MySqlClient",
            typeof(MySqlClientFactory).AssemblyQualifiedName
        );

        // Register Entity Framework provider
        SetProviderServices("MySql.Data.MySqlClient", new MySqlProviderServices());
        SetDefaultConnectionFactory(new MySqlConnectionFactory());
    }
}

The first part of the configuration is a hack to register the ADO.NET provider at runtime, by dynamically adding a new configuration entry to the system.data section. This is very hacky, but does appear to work correctly.

配置的第一部分是通过动态地向system.data部分添加新的配置条目来在运行时注册ADO.NET提供程序的hack。这非常hacky,但似乎确实正常工作。

Add the connection string to config.json rather than Web.config:

将连接字符串添加到config.json而不是Web.config:

{
  "Data": {
    "DefaultConnection": {
      "ConnectionString": "Server=localhost; Database=test; Uid=test; Pwd=password;"
    }
  }
}

Modify the DbContext to use the correct configuration and connection string:

修改DbContext以使用正确的配置和连接字符串:

[DbConfigurationType(typeof(MyDbConfiguration))]
public class MyContext : DbContext
{
    public MyContext(IConfiguration config)
      : base(config["Data:DefaultConnection:ConnectionString"])
      {
      }
      // ...
}

Register MyContext in the dependency injection container in Startup.cs:

在Startup.cs中的依赖注入容器中注册MyContext:

public void ConfigureServices(IServiceCollection services)
{
    // ...
    services.AddScoped<MyContext>();
}

Then you can just use constructor injection to get MyContext into your controllers.

然后你可以使用构造函数注入将MyContext导入控制器。

More details in my blog post at http://dan.cx/2015/08/entity-framework-6-mysql-aspnet, and a sample project at https://github.com/Daniel15/EFExample

有关详细信息,请访问我的博客文章http://dan.cx/2015/08/entity-framework-6-mysql-aspnet,以及https://github.com/Daniel15/EFExample上的示例项目