EF7多个datacontext

时间:2016-11-25 11:17:32

标签: c# asp.net entity-framework entity-framework-core

我有一个使用EF7和SQL Server 2014的Asp.Net 5 Web应用程序。现在我需要制作第二个连接到SQL Server 2008 R2的DBContext。出于某种原因,我能够连接但无法从数据库中获取任何数据。

config.json:

"Data": {
"PMSConnectionString": "Data Source=xxxx;User ID=xxxx;Password=xxxx;Initial Catalog=xxxx;",
"MTOCL2ConnectionString": "Server=xxxx;Database=xxxx;User Id=xxxx;Password=xxxx;"
} 

Startup.cs:

public Startup(IHostingEnvironment env)
    {
        // Setup configuration sources.
        var configurationBuilder = new ConfigurationBuilder()
            .AddJsonFile("config.json")
            .AddEnvironmentVariables();
     Configuration = configurationBuilder.Build();
    }



    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().AddJsonOptions(options =>
        {
            options.SerializerSettings.ContractResolver =
                new CamelCasePropertyNamesContractResolver();
            options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        });

        services.AddCaching(); // Adds a default in-memory implementation of IDistributedCache
        services.AddSession(s => s.IdleTimeout = TimeSpan.FromMinutes(360));

        services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<MTOCL2Context>(options =>
                options.UseSqlServer(Configuration["Data:MTOCL2ConnectionString"])
                ).AddDbContext<PMSContext>(options =>
                options.UseSqlServer(Configuration["Data:PMSConnectionString"]))

            ;

    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.MinimumLevel = LogLevel.Information;
        loggerFactory.AddConsole();
        loggerFactory.AddDebug();

        app.Use(async (context, next) =>
        {
            context.Response.Headers.Append("Cache-Control", "private, no-store");
            await next();
        });

        app.UseSession();

        app.UseIISPlatformHandler();

        app.UseStaticFiles();

        app.UseMvc();
    }

MTOCL2Context.cs:

public class MTOCL2Context:DbContext
{
    public DbSet<SubLotPriority> SubLotPriorities { get; set; }
}

UserController中:

[Route("api/[controller]")]
public class UsersController : Controller
{
    private readonly PMSContext _dbContext;
    private readonly MTOCL2Context _dbContext2;

    public UsersController(MTOCL2Context dbContext2, PMSContext dbContext)
    {
        _dbContext2 = dbContext2;
        _dbContext = dbContext;
    }
}

当我运行应用程序并尝试从SubLotPriority获取数据时,我收到错误:

  

评估方法System.Linq.SystemCore_EnumerableDebugView&#39; 1 [[SublotPriority ...

我实际上没有收到任何错误,当我进入dbcontext时,我没有得到任何结果,并且屏幕上显示错误消息。不知道如何获得完整的信息: Error Message

0 个答案:

没有答案