5中如何使用Entity

5中如何使用Entity

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

问题描述

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

解决方案

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

  public class MyDbConfiguration:DbConfiguration 
{
public MyDbConfiguration()
{
//注册ADO.NET提供者
var dataSet =(DataSet)ConfigurationManager.GetSection(system.data);
dataSet.Tables [0] .Rows.Add(
MySQL数据提供者,
.Net框架数据提供者为MySQL,
MySql.Data.MySqlClient ,
typeof(MySqlClientFactory).AssemblyQualifiedName
);

//注册实体框架提供者
SetProviderServices(MySql.Data.MySqlClient,新的MySqlProviderServices());
SetDefaultConnectionFactory(new MySqlConnectionFactory());
}
}

配置的第一部分是一个黑客注册ADO.NET提供程序通过动态地向 system.data 部分添加新的配置条目。这是非常诡异的,但确实似乎正常工作。



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

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

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

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

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

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

然后你可以使用构造函数注入来获取 MyContext 进入您的控制器。



我的博客文章中的更多详细信息,请访问,以及


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?

解决方案

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

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.

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

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

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"])
      {
      }
      // ...
}

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

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

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

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 13:11