Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use Entity Framework 6.x in Asp.Net 5 (MVC 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?

like image 676
mikal Avatar asked Mar 27 '15 08:03

mikal


People also ask

How can add Entity Framework in ASP.NET MVC?

Right-click the Controllers folder in Solution Explorer, select Add, and then click New Scaffolded Item. In the Add Scaffold dialog box, select MVC 5 Controller with views, using Entity Framework, and then choose Add.

Can I use Entity Framework with .NET 5?

NET 5 SDK. Technically, EF Core 5 can run on . NET Core 3.1, but aligning versions is always a good idea. Starting with a brand new console application, we will need to install the following packages, making sure that all the versions are 5.0.

Can we use Entity Framework 6 in asp net core?

To use Entity Framework 6, your project has to compile against . NET Framework, as Entity Framework 6 doesn't support . NET Core. If you need cross-platform features you will need to upgrade to Entity Framework Core.


3 Answers

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());
like image 158
Tom Avatar answered Oct 16 '22 18:10

Tom


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());
        SetProviderServices("MySql.Data.MySqlClient",
                    new MySql.Data.MySqlClient.MySqlProviderServices());
    }
}

PostgreSql needs to register provider into entityFramework AND system.data 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: http://bleedingnedge.com/2015/11/01/entity-framework-6-with-asp-net-5/

like image 6
pg0xC Avatar answered Oct 16 '22 18:10

pg0xC


Can you not just do this in the startup.cs file? Save creating a factory

// new context on each request
services.AddScoped<IMyContext, MyContext>((s) =>
{
    return new MyContext(Configuration["Data:MyConnection:ConnectionString"]);
});
like image 5
Gillardo Avatar answered Oct 16 '22 18:10

Gillardo