实体框架6不识别AddSqlServer() [英] Entity Framework 6 not recognizing AddSqlServer()

查看:145
本文介绍了实体框架6不识别AddSqlServer()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用EF6与VS2015 CTP和ASP.NET MVC 6.它不承认下面提到的代码在 startup.cs ,当我是使用EF7。

  public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFramework(Configuration)
.AddSqlServer()
.AddDbContext< VNDBContext>(options => options.UseSqlServer(Configuration.Get(Data:VNDBContext:ConnectionString)));

services.AddMvc()。配置< MvcOptions>(options =>
{
var jsonFormatter =(JsonOutputFormatter)options.OutputFormatters
.Where(o = > o.Instance.GetType()== typeof(JsonOutputFormatter))。First()。Instance;
jsonFormatter.SerializerSettings.ReferenceLoopHandling =
ReferenceLoopHandling.Ignore;

} );
services.AddSingleton< INodeService,NodeService>();
}

如果我使用EF7,它还有其他问题,因为它还不成熟。



如何在MVC6中进行所有设置,但使用EF6?如何设置上述设置?

解决方案

由于@ErikEJ在回答中提到,EF6与EF7非常不同,在Startup.cs中没有任何工具。但是,这并不是说不可能;我正在使用自己的项目,使用类似于以下内容的行:

  services.AddTransient< MyEf6DbContext>(sp = > new MyEf6DbContext(Configuration.Get(Data:VNDBContext:ConnectionString))); 

这样可以使用 MyEf6DbContext 你的控制器和其他服务就像你所期望的那样!您将不得不做更多的工作来获得身份3,并与EF6配合使用,但都是非常可插拔的。


I am trying to use EF6 with VS2015 CTP and ASP.NET MVC 6. it is not recognizing the below mentioned code in startup.cs which I added when I was using EF7.

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddEntityFramework(Configuration)
          .AddSqlServer()
          .AddDbContext<VNDBContext>(options => options.UseSqlServer(Configuration.Get("Data:VNDBContext:ConnectionString")));

        services.AddMvc().Configure<MvcOptions>(options =>
        {
            var jsonFormatter = (JsonOutputFormatter)options.OutputFormatters
            .Where(o => o.Instance.GetType() == typeof(JsonOutputFormatter)).First().Instance;
            jsonFormatter.SerializerSettings.ReferenceLoopHandling =
                 ReferenceLoopHandling.Ignore;

        });
        services.AddSingleton<INodeService, NodeService>();
    }

If I use EF7 it has other issues as it is not matured yet.

How can I set above mentioned setting while doing everything in MVC6 but with EF6?

解决方案

As @ErikEJ mentions in his answer, EF6 is very different from EF7 and does not have any utilities for working in the Startup.cs. However, that is not to say it's not possible; I'm doing it with my own project with a line similar to the following:

services.AddTransient<MyEf6DbContext>(sp => new MyEf6DbContext(Configuration.Get("Data:VNDBContext:ConnectionString")));

This allows you to use MyEf6DbContext injected into your controllers and other services just as you'd expect! You'll have to do some more work to get Identity 3 and such to work with EF6, but they're all very pluggable.

这篇关于实体框架6不识别AddSqlServer()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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