Shared library for asp.net core app and windows service - entity-framework

I have an ASP.NET Core web app and a Windows Service that both access the same database. I am using Entity Framework Core as an ORM. I would like to be able to create a library package that encapsulates all data access (ie. a Repository) and can be shared by both the web app and the service.
Entity Framework Core appears to only be supported in DNX applications, and Windows Services, as standard Windows applications, cannot reference DNX projects. Is there any way of creating a DNX Windows Service, or another approach that would work?
Creating a standard windows class library with EF 6.x is not an option as the Repository has already been written in EF Core.

Just discovered my mistake, Entity Framework Core is supported in standard windows projects, so it looks like I have my solution: create a Repository class library that accesses the database using EF Core and reference it from both the ASP.NET Core and Windows Service apps.

Related

Migrating Web Api to .Net core

I have a .Net FW 4.6 web API project that I would like to upgrade to .Net core.
Currently the project is using ADO library to call stored procedures in the SQL database.
I see that in .Net Core everyone is using Entity framework. Do I have to use it if I want to just call stored procedures?

Use ADO.NET Entity Model (.edmx) in Blazor .Net Framework

I like to use a previous ADO.Net Entity Model, stored in another project (in the same solution) and imported like DLL in Blazor's ASP.NET Framework (not Core).
Unfortunately when i try to connect my context I receive that error: "No connection string named 'MyDBEntities' could be found in the application config file.".
I tried to configure appsettings.json:
"ConnectionStrings": {
"MyDBEntities": "xxxxxxxxxxxxxxxxxxxx"
}
and the startup.cs:
services.AddScoped<MyDBEntities>((_) => new MyDBEntities(Configuration.GetConnectionString("MyDBEntities")));
What am I doing wrong?
After some research, the best solution for implement a system like ADO.NET in .Net 6 with Blazor is to use EF Core Power Tools, a useful design-time DbContext features for connect your DB (in my case is SQL Server) and automate the creation or update of your tables.
It's very simple to install, configure and use like ADO.NET.
For more information about how install in your Visual Studio Solution, here is the link:
https://marketplace.visualstudio.com/items?itemName=ErikEJ.EFCorePowerTools

linq to sql upgrade path?

I have a .Net Framework library project containing a bunch of models and business logic based off Linq 2 Sql. I currently have a .Net Framework website consuming it. I want to create a new .Net Core Web API and access this library.
What do I upgrade this library to that will allow it to be consumed by both .Net Framework and .Net core? Is there a ".Net Standard Entity Framework?" I can only see .Net Core EF. So I am at a loss as to how to share this logic layer with my old .net framework and new .net core website.
EF 6.3 runs on .net standard and compatible with Framework and Core.

Stateless worker service and api service both referencing same project

I have a service fabric solution that contains the following:
Worker service (.net framework 4.7)
Api Service (.net core 2.0 as VS 2017 does not provide a template for framework api services)
Infrastructure project (.net standard) referencing EntityFrameworkCore
Both services reference infrastructure project.
This gives me issues as the framework service will not load EntityFrameworkCore reference due to it being core.
How do I go about resolving this issue?
Can I force the core to load EF 6.2?
Can I force 4.7 to load EFCore?
Entiy Framework Core is .NET Standard as well, it should load fine on .NET Framework 4.7.
You may need to add the EF Core NuGet packages to the worker service if it doesn't yet use the transitive NuGet package reference style.

Use EF6 with ASP.NET Core

I am working a new project - and I want to do it using the latest .NET Core.
Reading through the documentation, I came across the comparison between Entity Framework Core and Entity Framework 6.x Comparison. Here
Some of the features that I require like: Spatial Data Type is NOT supported by EF Core. So I CAN'T use it, because Spatial Data Type is required in the Project.
Going through the documentation again, I came across this link on How to use EF 6.x with .NET Core: Here
In the article it mentions (Here):
Before you start, make sure that you compile against full .NET
Framework in your project.json as Entity Framework 6 does not support
.NET Core. If you need cross platform features you will need to
upgrade to Entity Framework Core.
I don't really need my project to be cross platform.
The article is not very clear on how to use EF6 with ASP.NET Core.
I created a new ASP.NET Core Web Application
then I installed EF6 using Nuget Package Manager, and got the following error
I went and removed the netcoreapp1.0 from the frameworks in project.json and added net46 as framework, which removed the error for EF, but now I am getting another error.
So how do I use EF6 with ASP.NET core?
When I use EF6 I have to remove all references to .NET Core, does
this mean I am not using .NET Core? What are the disadvantages of this other than it can't run cross platform?
To stop the second error you need to remove the following from the dependencies section of your project.json:
"Microsoft.NETCore.App": {
"version": "1.0.0",
"type": "platform"
}
For reference, the documentation for referencing the full .NET Framework and Entity Framework 6 from an ASP.NET Core project has a full project.json example.
Removing references to .NET Core does not mean you are not using ASP.NET Core. You are only changing the target runtime platform that your ASP.NET Core project will execute on.
Using ASP.NET Core with the full .NET Framework, you get the benefits of the new project structure and unified story for building web UI and web APIs (e.g. unified Controller class), and you also gain access to the mature, fully-featured .NET Framework, enable use of dependencies and NuGet packages which haven't been ported to .NET Core, for example Entity Framework 6.