Reference an EF5 project from an EF6 project - c#

Is it possible to use a project that was written and built using Entity Framework 5 as a reference in a project that uses Entity Framework 6? When doing so, I get "Method not found" errors on functionality that has been deprecated in EF6, but that works just fine when used in the EF5 project as a standalone project.
A call like this:
dataContext.Database.SqlQuery()
throws an error of:
{"Method not found: 'System.Collections.Generic.IEnumerable`1<!!0> System.Data.Entity.Database.SqlQuery(System.String, System.Object[])'."} System.Exception {System.MissingMethodException}

Related

Problem with HealthCheckers-UI after migration to .Net Core 6 [duplicate]

I used the database first approach.
The model is right (or at least it looks like)
But I always get this error. Please, I've already tried so many things..
The full code of my program (and even sql script by which I create my database)
is here: https://github.com/AntonioParroni/test-task-for-backend-stack/blob/main/Server/Models/ApplicationContext.cs
Since I have a mac. I created my model with dotnet ef cli commands (dbcontext scaffold)
I can use my context. But I can't touch any DbSet..
public static void Main(string[] args)
{
using (ApplicationContext context = new ApplicationContext())
{
Console.WriteLine(context.Database.CanConnect());
var months = context.Months.ToList();
foreach (var month in months)
{
Console.WriteLine(month.MonthName);
}
}
//CreateHostBuilder(args).Build().Run();
}
It is not my first time using EF. And everything was working fine before, in many simple projects or tasks. While here.... It doesn't matter what I do (I even tried to rename all of my columns name, erase all tables except one, modify the context code, use the same steps from this project on a new, totally empty project..)
It is always..
Unhandled exception. System.TypeInitializationException: The type initializer for 'Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor' threw an exception.
---> System.TypeInitializationException: The type initializer for 'Microsoft.EntityFrameworkCore.Query.QueryableMethods' threw an exception.
---> System.InvalidOperationException: Sequence contains more than one matching element
at System.Linq.ThrowHelper.ThrowMoreThanOneMatchException() in System.Linq.dll:token 0x600041c+0xa
ect....
Here is my package reference file
"restore":{"projectUniqueName":"/Users/mac/Documents/GitHub/test-task-for-backend-stack/Server/Server.csproj",
"projectName":"Server","projectPath":"/Users/mac/Documents/GitHub/test-task-for-backend-stack/Server/Server.csproj",
"outputPath":"/Users/mac/Documents/GitHub/test-task-for-backend-stack/Server/obj/","projectStyle":
"PackageReference","originalTargetFrameworks":["net6.0"],"sources":{"https://api.nuget.org/v3/index.json":{}},
"frameworks":{"net6.0":{"targetAlias":"net6.0","projectReferences":{}}},
"warningProperties":{"warnAsError":["NU1605"]}}"frameworks":{"net6.0":
{"targetAlias":"net6.0","dependencies":{"EntityFramework":
{"target":"Package","version":"[6.4.4, )"},
"Microsoft.EntityFrameworkCore.Design":{"include":"Runtime, Build, Native, ContentFiles, Analyzers, BuildTransitive",
"suppressParent":"All","target":"Package","version":"[5.0.0, )"},
"Microsoft.EntityFrameworkCore.SqlServer":{"target":"Package","version":"[5.0.0, )"},
"Swashbuckle.AspNetCore":{"target":"Package","version":"[5.6.3, )"}},
"imports":["net461","net462","net47","net471","net472","net48"],
"assetTargetFallback":true,"warn":true,
"frameworkReferences":{"Microsoft.AspNetCore.App":{"privateAssets":"none"},
"Microsoft.NETCore.App":{"privateAssets":"all"}},
"runtimeIdentifierGraphPath":"/usr/local/share/dotnet/sdk/6.0.100-preview.6.21355.2/RuntimeIdentifierGraph.json"}}
It's been already a few days. And I'm becoming really confused and mad.
Why is this happening.. and why there is not that much info about this type of error in the internet. Please, just point me in the right direction..
You have net6.0 target framework which is still not released while you have installed EF6 which is a previous iteration Entity Framework (mainly used with legacy .NET Framework projects) and you also have EF Core (a modern iteration of it) but older version - 5.0 (which you are actually using for your context, see the using Microsoft.EntityFrameworkCore; statements there).
Try removing EntityFramework package and installing preview version of Microsoft.EntityFrameworkCore.SqlServer (possibly just updating to the latest 5 version also can help) and either removing completely or installing preview version of Microsoft.EntityFrameworkCore.Design. (Also I would recommend to update your SDK to rc and install rc versions of packages).
Or try removing the reference to EntityFramework (not Core one) and changing target framework to net5.0 (if you have it installed on your machine).
As for why do you see this exception - I would guess it is related to the new methods added to Queryable in .NET 6 which made one of this checks to fail.
TL;DR
As mentioned in the comments - update EF Core to the corresponding latest version (worked for 5.0 and 3.1) or update to .NET 6.0 and EF Core 6.

Reference manager is empty in vs 2017 .net core class library project

I'm trying to add reference to System.Configuration.dll assembly.
I'm using .Net Core, as it can be seen in my .csproj file:
<TargetFramework>netcoreapp2.2</TargetFramework>
(Output type of my project is set to Class Library.)
To do this, in solution explorer, I right-click on Dependencies of my project and then click on Add Reference.... Problem is that the opened Reference Manager is empty and there is no assembly I can add a reference to.
However, without referencing that assembly I'm able to write the following line without any build error:
using System.Configuration;
First Question: How is it possible that the above line gets successfully built, without me referencing that assembly?
If I try to use ConfigurationManager class to read data from app.config file (As told here ), I get an error complaining that:
CS0103 The name 'ConfigurationManager' does not exist in the current context
Question two: what can I do to solve that error? what's wrong with my code or project?
My case is not the same as this question and this is not a duplicate of that question.
Add Reference is for your local references. You need to use Nuget Package Manager in order to include references.
most importantly .Net Core does not use .Net Framework packages where ConfigurationManager is .Net Framework package.
You need to use .Net Core or .Net Standard libraries with .Net Core

Ambiguous error between TableNameAttribute.TableNameAttribute(string)

I just upgraded the .Net v4.0 to 4.6.1 and ended up having the ambiguous error for the sql related attributes(TableName, PrimaryKey, Column) I used in the application.
I am trying to figure out which DLL is causing this error.I am seeing the errors like
TableNameAttribute.TableNameAttribute(string)
PrimaryKey.
ColumnAttribute...etc
I want to know which dll I have to delete
the ambiguity was between the Entity framework and the System.componentmodel.dataannotations

Entity Framework setup issue

When I run my code
using (foodEntities db_Linq = new foodEntities())
{
return db_Linq.Database.Exists();
//return db_Linq.Foods.Any();
}
I keep getting this error in the output window.
A first chance exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll
What could be the issue? DB Credentials in web.config?
Other info:
I actually have 2 projects in my solution. One's a class library, the other MVC 4 web project.
Class library uses Entity Framework connection. Web project uses ADO connection string.
The above issue happens when web project calls the class library.
The code given above is from class library.
What version of the EF are you using? It seemed to me that you use EF6 and you have issues with the namespaces.
Please, check Updating Applications to use EF6 link. The main part for you is "Update namespaces for any core EF types being used"

Add project as reference to another project

I have two projects in the same solution.
One is an ASP.NET MVC 2 Empty Web Application,
the other is a Console Application.
In the first project i create a database with EF and a class to access the database.
What I want to do is add the first project as a reference to the console app project so i can use this class
I included it as a reference already it appears at the References, and after is include a using directive at the top of my console app i can even use code assistance. But when i compile i always get the error:
The type or namespace name 'DBModel' could not be found (are you missing a using directive or an assembly reference?)
How can i compile my projects?
Sincerely,
Zoli
From your description, if I'm getting it right,
That's often the case when you have different target .NET framework (Project properties, Application, Target Framework) - i.e. one is e.g. .NET 4.0 Client Profile - and the lib you're referencing is just .NET 4.0.
It's also possible that you're missing a reference to the Entity Framework in your Console application.
See this post for more info: http://www.dotnetcurry.com/ShowArticle.aspx?ID=617 (just below the middle, step 3).

Categories

Resources