how to use EF6.0 in .netframework 4.6.2 class library project referenced inside ASPNET Core 2.0 project - entity-framework-6

I am trying to use EF6.0 in Class Library Project .Net Framework 4.6.2 (Repository project) where the DBContext live next to the repository and the Entities have been created in different project(.NET framework 4.6.2) using Reverse Code First POCO,
The Repository will be called thru another (framework 4.6.2) Manager Project referenced inside ASPNET Core 2.0 SPA Project, where StructureMap used as IoC.
The first thing I faced was the issue of passing the connectionstring to the DBContext inside Repository Project, as I want to Register the services (DBContext,Repository) inside the Repository Project not from the ASPNET Core Startup.cs, the reason to that is to avoid exposing the DBContext to the frontend project, anyway I ended up hard-coding the connection string in Repo Project temporarily, "appreciate if there is another way!"
The main issue that I am facing with this design comes when the request send to collect the list of "Products", it reaches to the Repository Project where the Linq statement use _dbcontext.Product , then error message thrown
NotSupportedException: Unable to determine the provider name for provider factory of type 'System.Data.SqlClient.SqlClientFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.
The Entityframework is registered inside Repository project, but not inside ASPNET Core 2.0 Project as it will show Nuget conflict error when its installed in ASPNET Core 2.0.
also I tried to install "System.Data.SqlClient" from Nuget in ASPNET Core2.0 but the error still throwing.
Any Idea on how to use EF6.0 in .netframework 4.6.2 class library project referenced inside ASPNET Core 2.0 project.

The Microsoft article Get Started with ASP.NET Core and Entity Framework 6 has the steps necessary for using Entity Framework 6 from a ASP.NET Core project.
Regarding the NotSupportedException, I would refer to the related question System.NotSupportedException: Unable to determine the provider name for provider factory of type 'System.Data.SqlClient.SqlClientFactory'.
I ran into the same issue because I failed to change the Core .csproj file from <TargetFramework>netcoreapp2.2</TargetFramework> to <TargetFramework>net471</TargetFramework> (which the article told me to do, but for some reason I thought wasn't necessary in Core 2.2).
I provided more explanation and details in the posted explanation/answer to the question I referenced above.

Related

EntityFramework (database first) using Azure managed identity for SQL DB

I'm in the process of converting the existing .Net framework project that uses EF 6 with Database First approach (EDMX) file to .Net Standard 2.1 which connects to SQLDB hosted on Azure.
Existing EDMX file has a constructor (auto generated) that accepts ConnectionString as given below -
public TestDatabaseConnection()
: base(ConfigurationManager.ConnectionStrings["TestDatabaseConnection"].ConnectionString)
{
}
This is breaking for two reasons -
ConfigurationManager.ConnectionStrings works only in .Net framework projects and throw runtime errors in the .Net Core project (for eg- ConfigurationManager.ConnectionStrings returns null).
I need to make changes to the above code to use the AppAuthentication nuget as mentioned in the MSDN docs here
So, what are the options available in order to make this work so that I could use the same nuget package for connecting to the database via EF 6 (database first) for both .net framework and .net core projects?
Is the only available is to convert to Code first and make the necessary changes as mentioned in the above MSDN link?

The type or namespace name 'IClientValidatable' could not be found - in .NET 4.7

I'm building an ASP.NET MVC app with VS 2017, targeting .NET 4.7, and I'm trying to build my models inside of business library being referenced by the web application. So I'm trying to get many of the same classes that come with an MVC project by default into a brand new class library.
Particularly, I'm getting the following error:
This is very different from the previous times this question was posted:
The type or namespace name 'IClientValidatable' could not be found
The type or namespace name 'IClientValidatable' could not be found (are you missing a using directive or an assembly reference?)
Namely, in that they both are solved simply by including the using statement for System.Web.Mvc where IClientValidatable lives.
But I've definitely already done that... Here's the reference manager for the class library with the reference included available for .NET 4.7
As further proof, here's a side by side example of where I can pull in some classes from System.Web.Mvc, but not IClientValidatable
For reference sake, here's the configuration on the class library itself
So did this class move somewhere? Is it available with .NET 4.7?
So the real question seems to be not where did it go, but why the most recent framework is targeting such an old version.
When trying to add a reference to System.Web.MVC while targeting .NET Framework 4.7, the only available option is MVC v2.0.0.0 which is incredibly old. When seeding a new MVC app, it'll come with v5.2.3.0
Here's the Assembly Explorer with both libraries loaded. Common items will be highlighted in each, but IClientValidatable wasn't added until later.
And here's the VS 2017 Reference Manager where you can add references based on your current framework, showing the old MVC library for the new .NET version.
So the question then becomes....
Q: How can I add a reference that is not available in the list of assemblies for my framework?
A: Same way you'd add any other reference - you can browse to a dll or grab it from nuget.
DLL If you already have a web app, there are good odds you can find the dll in your existing packages directory at something like:
\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll
If you prefer Nuget, some minor annoyances are that nuget package names don't exactly sync up with the assemblies they install, it's not easy to browse or search which assemblies come with which packages, and they libraries and frameworks often come bundled with other things you might not need. All that said, you'll be looking for the package called:
Microsoft.AspNet.Mvc on Nuget
Which will install the following libraries:

How to use Unity MVC & Unity WebAPI in the same ASP.Net MVC project

I'm trying to add a WebAPI to an existing ASP.Net MVC project and my question is regarding Unity:
Before with UnityMVC alone, the project had in the App_Start folder, the class UnityConfig with the following code:
container.RegisterType<IUnitOfWork, UnitOfWork>(new PerRequestLifetimeManager());
container.RegisterType<IDataContext, tfe_schemaContext>(new PerRequestLifetimeManager());
container.RegisterType<IPrintJobService, PrintJobService>(new PerRequestLifetimeManager());
container.RegisterType<IRepository<print_job>, Repository<print_job>>(new PerRequestLifetimeManager());
To use a WebAPI, I added UnityWebAPI via NuGet and this action added UnityMvcActivator class in the App_Start folder.
The following error displayed when I execute the project:
"The type Unity.WebApi.UnityDependencyResolver does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator.
Parameter name: commonServiceLocator".
I found different examples of code using a Bootstrapper class but without using the UnityConfig class or UnityMvcActivator.
In the Bootrstrapper class, the registration of the types is like this:
container.RegisterType<IProductServices, ProductServices>().RegisterType<UnitOfWork>(new HierarchicalLifetimeManager());
I'm a little bit lost with all this, please help me understand and fix this error.
It seems you are using wrong or outdated NuGet packages. Try use Unity's official packages instead. Therefore remove any packages related to Unity and add following packages in Package Manager Console:
Install-Package Unity.Mvc
Install-Package Unity.AspNet.WebApi
Now you have one Unity container in your project which works well with MVC and WebAPI.
You can also try by separating the projects.
One project four you MVC code and another project for your WebAPI code. That way you will install Unity.MVC in your MVC project and Unity.WebAPI in your WebAPI project.
After that, you can consume the services from your MVC Application without any problem.

..Net MVC4 Method not found: 'Void System.Data.Objects.ObjectContextOptions.set_UseConsistentNullReferenceBehavior(Boolean)'

I am developed a website using VS Express for Web 2012 with target framework .NET framework 4.0
I am adding a controller using entity framework and i run well in my local computer.
However when i upload it to my hosting, i got this error:
Method not found: 'Void System.Data.Objects.ObjectContextOptions.set_UseConsistentNullReferenceBehavior(Boolean)'.
In my web.config it show that the reference use is Entity Framework 5.0
Do i need to change it to Entity Framework 4.0 (since maybe my hosting doesn't support version 5.0)?
How to update the reference? I am new to .NET MVC and kinda confuse with NuGet things.
Thanks in advance.

Using Ninject's InRequestScope in a class library referenced by MVC

My solution is divided as follows:
Data project - holds Entity Framework
Business Logic/Services project - contains classes that implement business logic/do other work on the data
MVC3 project
The way I have this set up is the services class does work involving entity framework. I'm using dependency injection for creating the repository wrapping Entity Framework. The problem I'm running into is that each time the repository is created via ninject, it's creating a new EF context so not all changes are being saved. Note that I have Ninject bindings in both the services project and the MVC project, and the instance I'm talking about here is when the bindings are located in the class library.
Based on the research I've done, it seems to be recommended to use InRequestScope so that way the same context gets used. However, since I'm using this in a class library instead of the MVC project/web project, does it make sense to use Ninject.Web.Common in the class library (where it goes and creates AppStart folders and everything)?
Or is there another way I should handle this?
I was misunderstanding how Ninject.Web.Common worked and I was getting confused by the auto-added NinjectWebCommon cs file that was automatically added via nuget install, making me think that it was only for the entry point project. I wasn't aware that my class library would have access to HttpContext and by getting rid of the AppStart folder that the nuget package "helpfully" added, I was able to use InRequestScope in my class library.

Resources