如何在 ASP.NET 5 中将 Entity Framework 6 与 MySQL 一起使用? [英] How do I use Entity Framework 6 with MySQL in ASP.NET 5?

查看:30
本文介绍了如何在 ASP.NET 5 中将 Entity Framework 6 与 MySQL 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 5 不再使用 Web.config,您需要使用 基于代码的配置 来配置它.为此,请创建一个继承自 DbConfiguration 的新类:

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:

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

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

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.

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

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

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

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

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

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

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

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

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

然后您可以使用构造函数注入将 MyContext 放入您的控制器中.

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

更多细节在我的博客文章 http://dan.cx/2015/08/entity-framework-6-mysql-aspnet,以及 https:///github.com/Daniel15/EFExample

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

这篇关于如何在 ASP.NET 5 中将 Entity Framework 6 与 MySQL 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆