带有ASP MVC .NET 4.6的EF Core [英] EF Core with ASP MVC .NET 4.6

查看:94
本文介绍了带有ASP MVC .NET 4.6的EF Core的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个项目中,我需要设置一个ASP.NET MVC(带有.NET 4.6.1),但是要使用新的" EF Core来访问数据库.

In a project I need to set up an ASP.NET MVC (with .NET 4.6.1) but using the "new" EF Core for accessing the database.

不幸的是,每个文档都只解释了如何设置ASP.NET Core MVC项目.

Unfortunately every documentation explains only how to setup an ASP.NET Core MVC project.

我只是尝试了一下,当通过Package Manager控制台创建数据库时,出现错误消息:

I just gave it a try and when it comes to creating the database via the Package Manager Console I get the error message:

在'DataContext'上找不到无参数的构造函数.要么添加一个 无参数的构造函数到'DataContext'或添加一个实现 "IDbContextFactory"与"DataContext"位于同一程序集中

No parameterless constructor was found on 'DataContext'. Either add a parameterless constructor to 'DataContext' or add an implementation of 'IDbContextFactory' in the same assembly as 'DataContext'

是的,我没有无参数的构造函数,但是Microsoft的示例代码也没有

Yes, I don't have a parameterless constructor but neither does the example code of microsoft

public DataContext(DbContextOptions<DataContext> options) : base(options) { }

public DataContext(DbContextOptions<DataContext> options) : base(options) { }

我想问题是,我没有在旧" ASP.NET MVC应用程序中没有的Startup.cs中注册DataContext.

I guess the problem is, that I don't register the DataContext in the Startup.cs which I don't have in an "old" ASP.NET MVC application.

有人可以帮我吗?

推荐答案

一个简单的示例

  • Example.EF (具有EF Core和Microsoft Dependency Injection的.NET Standard项目).
  • Example.Tools -参考Example.EF (.NET Core CommandLine项目,仅针对开发人员运行迁移).
  • Example.MVC -参考Example.EF (MVC5).
  • Example.EF (.NET Standard project with EF Core and Microsoft Dependency Injection).
  • Example.Tools - reference Example.EF (.NET Core CommandLine project to run the migration only for the developer only).
  • Example.MVC - reference Example.EF (MVC5).

Example.EF 中:安装EF Core,Microsft依赖注入.创建一个支持DI的类

In Example.EF: Install EF Core, Microsft Dependency Injection. Create a class to support DI

public static class IocConfiguration
{
    public static void Configure()
    {
        var services = new ServiceCollection();

        services.AddDbContextPool<ExampleContext>(options => {
            options.UseSqlServer("_connectionstring_");
        });

        // Register to support the ExampleController can get DbContext.
        services.AddTransient(typeof(ExampleController));

        var serviceProvider = services.BuildServiceProvider();
        DependencyResolver.SetResolver(new DefaultServiceResolver(serviceProvider));
    }
}

public class DefaultServiceResolver : IDependencyResolver
{
    private readonly IServiceProvider _serviceProvider;

    public DefaultServiceResolver(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    public object GetService(Type serviceType)
    {
        return _serviceProvider.GetService(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return _serviceProvider.GetServices(serviceType);
    }
}

Example.MVC 中,在Global.asax中使用Application_Start或在Owin中使用Startup

In the Example.MVC, with the Application_Start in Global.asax or Startup with Owin

// Register services.
IocConfiguration.Configure();

// Example controller
public class ExampleController : Controller 
{
     private readonly ExampleContext _exampleContext;

     public ExampleController(ExampleContext exampleContext)
     {
         _exampleContext = exampleContext;
     }
}

要运行迁移,请执行以下操作:

To run the migration:

Add-Migration {MigrationName} -Project Example.EF -StartupProject Example.Tools

我们应该有IDesignTimeDbContextFactory支持运行迁移.

We should have IDesignTimeDbContextFactory to support to run the migration.

这篇关于带有ASP MVC .NET 4.6的EF Core的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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