1

We spend thousands of hours creating application using EF6, and the structure we followed used EF6 framework into a separate class library layer, we are trying to switch to MVC 6 but still want to use EF6 class library project.

The problem we are currently facing is the connection string, we tried adding the connection string in applicationsettings.json and then updated the Startup.cs file accordingly, as suggested in the similar post How to Use Entity Framework 6.x in Asp.Net 5 (MVC 6)

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

var context = new MyContext("myConnectionString");

but nothing seems to be working, not sure how to use the below lines in my .net 4.6 class library project or how can I access the applicationsettings.json file?

IConfiguration configuration = new 

Configuration().AddJsonFile("config.json");
var connectionString = configuration["Data:DefaultConnection:ConnectionString"]);
1
  • You can just open the file and read it in. Or you can put the connection string somewhere else if it helps. It doesn't have to be there. But nothing should be different if it's a separate library. Commented May 2, 2016 at 5:26

1 Answer 1

2

Option 1

public class MyContext : DbContext
{
    public MyContext()
        : base(new Configuration().AddJsonFile("config.json")["Data:DefaultConnection:ConnectionString"]);
    {
    }
{

Option 2

public class MyContext : DbContext
{
    static readonly string ConnectionString;
    static MyContext()
    {
        IConfiguration configuration = new Configuration().AddJsonFile("config.json");
        ConnectionString = configuration["Data:DefaultConnection:ConnectionString"]);
    }

    public MyContext()
        : base(ConnectionString);
    {
    }
}

Option 3

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


public static class ContextFactory
{
    static readonly IConfiguration Configuration;
    static ContextFactory()
    {
        Configuration = new Configuration().AddJsonFile("config.json");
    }

    public static MyContext CreateMyContext()
    {
        return new MyContext(Configuration["Data:DefaultConnection:ConnectionString"]);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

none of the above work for me; just want to make a simple call to a database to return rows; I'm wondering how to make sense of this mess. Simple connection and call to database. I have connection string that points at the right database, shows tables in Server Explorer. I have MVC 5 apps that are working. What is MS up to? I don't need to run on other operatings systems. I just need a simple web app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.