How to Use Entity Framework 6.x in Asp.Net 5 (MVC 6) c# entity-framework-6


I'm testing out the new Asp.Net 5, using VS 2015 CTP-6. Because of the lack of features in Entity Framework 7, I would prefer using EF6 for now.

I've tried removing EF7 and then applying EF6 in PM, like this:

Uninstall-Package EntityFramework
Install-Package EntityFramework -version 6.1.3

No errors returned, and the project.json file seems updated accordingly. Although, there are no DbContext available.

Is this at all possible? If yes, how should I proceed from here? Do I need web.config for EF6 compatibility?

4/10/2015 11:36:02 PM

Accepted Answer

Yes, this works fine.

You need to manually set the connection string when creating the context since it can't get it from the web.config

so you can do this

public class MyContext : DbContext {
    public MyContext(string connectionString) : base(connectionString) {

var context = new MyContext("myConnectionString");

if you want to get the connection string from the config.json, then try this

IConfiguration configuration = new Configuration().AddJsonFile("config.json");
var connectionString = configuration["Data:DefaultConnection:ConnectionString"]);

and if you want to inject the context into the DI Container, then I added a factory like this

public static class MyContextFactory
    public static MyContext GetContext() {
        IConfiguration configuration = new Configuration().AddJsonFile("config.json");
        return new MyContext(configuration["Data:DefaultConnection:ConnectionString"]);


and then added this in startup.cs

services.AddTransient<MyContext>((a) => MyContextFactory.GetContext());
4/10/2015 11:32:35 PM

Popular Answer

Depending on database used it may not be as easy as answered. If you are using MsSql then no configuration is needed and the accepted answer is perfectly fine. But using LocalDB may need some configuration.

For example MySql needs to register provider

[DbConfigurationType(typeof(CodeConfig))] // point to the class that inherit from DbConfiguration
public class ApplicationDbContext : DbContext

public class CodeConfig : DbConfiguration
    public CodeConfig()
        SetDefaultConnectionFactory(new MySql.Data.Entity.MySqlConnectionFactory());
                    new MySql.Data.MySqlClient.MySqlProviderServices());

PostgreSql needs to register provider into entityFramework AND section. This may be done by using System.Data.Entity.DbConfiguration.Loaded event. Same goes with Oracle.

Check this blog post that explains it in details:

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow