vany0114 / EF.DbContextFactory

With EF.DbContextFactory you can resolve easily your DbContext dependencies in a safe way injecting a factory instead of an instance itself, enabling you to work in multi-thread contexts with Entity Framework or just work safest with DbContext following the Microsoft recommendations about the DbContext lifetime.
http://elvanydev.com/EF-DbContextFactory/
MIT License
93 stars 21 forks source link

Add support for migrations to EFCore.DbContextFactory #2

Closed Inlustris closed 6 years ago

Inlustris commented 6 years ago

Heey Giovani (it's me again),

I'm trying to split my project in to multiple projects. Now i have a seperate project for my repository layer. I would like to do services.AddDbContextFactory on this project but it is a .netstandard project.

Only the project that is using the different libraries is configured as .netcore. This way i have to register the database at the application instead of the project where it came from. Is there a way to do the services.AddDbContextFactory from a .netstandard library that is implemented by a .netcore application.

Kind Regards, Luc

vany0114 commented 6 years ago

Hey Luc,

Entity Framework doesn't' have a specification on .Net Standard, so EF has his own implementation for .Net Framework and .Net Core (very different in both frameworks), so as you know, .Net Standard is the specification, but EF hasn't specifications on .Net Standard that's why the implementations are very different, actually, they are different "things", EF and EF Core, respectively. Having said that, there is no EF Standard specification and it can't target multiple frameworks. I imagine you went with .Net Standard because you want to support multiple frameworks, but if that's not the case, I recommend you to switch to .Net Core in order to you can use EF Core.

Inlustris commented 6 years ago

Hello Giovani,

I would like to create the .net standard library to get those multiple frameworks. But it looks like this is not possible the way i want to do it. The solution i used is to dependency inject my database in the .NetCore application. And not passing the responsibility to the library itself.

I have another question: When i use

Am i forgetting something or am i doing something wrong?

Kind regards, Luc

vany0114 commented 6 years ago

Hi Luc,

Can you provide me an example of what are you doing exactly, please?

Inlustris commented 6 years ago

Hello Giovani,

I'm trying to update my database by creating a new migration in EFCore. To create this migration there has to be a dbcontext present. It looks like when i'm using Services.AddDbContectFactory. The program does not recognize the database. When i use Services.AddDbContext it is recognized.

In the code block below you can see that I add my dbcontext twice, one in the shape of a factory one in the shape of the context itself.

webHostBuilder = webHostBuilder.ConfigureServices(services =>
    {
        services.AddMvc();
        services.AddDbContext<DeviseContext>(builder => builder.UseSqlServer(config.GetConnectionString("DefaultConnection")));
        services.AddDbContextFactory<DeviseContext>(builder => builder.UseSqlServer(config.GetConnectionString("DefaultConnection")));
        services.Configure<AppSettings>(config.GetSection("AppSettings"));
    });

If it is possible i would like to remove the AddDbContext. Because i only make use of the Factory in my code.

What i could do is add:

#if DEBUG
#Endif

This way i can create migration and update the database in debug but this is in my opinion not the way to go.

I tried several solutions to fix this but none of them work. When researching the problem. This problem comes up when migrating from a lower version to EF core 2.0. in this Link they talk about it.

But there is no solution to the problem that it is causing.

For now i will use the AddDbContext but like i said i would like to remove it because it is never used.

Kind regards, Luc

vany0114 commented 6 years ago

Luc,

I'm planning to release that feature tomorrow or in the worst case until Friday, in order to EFCore.DbContextFactory supports migrations implicitly and you don't have to implement IDesignTimeDbContextFactory for your context , so, I'll let you know when the new version will be available.

Thanks for share your issue and use the project!

Inlustris commented 6 years ago

Heey Giovani,

That is nice to hear. I'm looking forward to it.

Thank you for this feature.

vany0114 commented 6 years ago

@Inlustris the new version is released, you can update it!

Inlustris commented 6 years ago

Heey Giovani,

Thank you for the update. I will use it in the upcoming days.