References Assembly EntityFramework - c#

I'm working on one of those new "Class Library (NuGet Package)" projects in Visual Studio. Everything was going fine until the other day it started raising an error about a:'assembly 'EntityFramework.Core, Version=7.0.0.0 reference??
How can I install the 7.0 version of EntityFramework into my Nuget packages??

"This issue" says that you need to add a reference to:
assembly 'EntityFramework.Core, Version=7.0.0.0
You can solve it by running the following command in the Package Manager Console:
Install-Package EntityFramework.Core -Pre

The issue is the EF version is different and following 2 assemblies are not supported.
using System.Data.Objects;
using System.Data.Objects.DataClasses;
Comment the above 2 lines and the following line in the code behind of your .edmx
using System.Data.Entity.Core.Objects;

Related

Error XA2002: Can not resolve reference: `Microsoft.Bcl.AsyncInterfaces`

I want to run my app in xamarin.forms but I faced this problem
Error XA2002: Can not resolve reference: Microsoft.Bcl.AsyncInterfaces, referenced by SimpleInjector. Please add a NuGet package or assembly reference for Microsoft.Bcl.AsyncInterfaces, or remove the reference to SimpleInjector. 0
I'm using Couchbise.Lite packages as Database for my app
I'm using VS2022
I installed the package Couchbase.Lite on my side(with VS 2022) and figured out that the error only happened on Version 3.0.0.Using Version 2.8.6 or below is functioning well.

Unable to use nuget packages

I have written some code that uses the following packages:
using System;
using System.IO;
using System.Reflection;
using Microsoft.Azure.WebJobs;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
But when I compile the code there is a few errors related to the following code, that I suppose it is written by the "Microsoft.Data.SqlClient" package:
using Microsoft.Data.Tools.Schema.Sql.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
namespace GetDatafromBlob0201
{
[TestClass()]
public class SqlDatabaseSetup
{
[AssemblyInitialize()]
public static void InitializeAssembly(TestContext ctx)
{
// Setup the test database based on setting in the
// configuration file
SqlDatabaseTestClass.TestService.DeployDatabaseProject();
SqlDatabaseTestClass.TestService.GenerateData();
}
}
}
Where "using Microsoft.VisualStudio.TestTools.UnitTesting" throws an error.
I have tried installing the NuGet package, but that didn't solve the error.
These are the errors I get:
Errors after compiling
Mostly CS0246.
The last one indicates that the reference could not be used.
I am also noticing a warning sign under my proyect, on "Packeges" and also on "Assembly":
The one on Assembly its on Microsoft.Data.Tools.Components, Version= 16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL. It doesn't give me details on the warning.
The one on Packages is on Microsoft.Data.Tools.UnitTest and it says: Package 'Microsoft.Data.Tools.UnitTest' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead the project target framework '.NETCoreApp,Version=v3.1'. This package may not be fully compatible with your project.
Test with your csproj file and install any nuget packages, I did not get any build errors.
These are the nuget packages which I have installed.
Microsoft.Data.SqlClient
Microsoft.Azure.WebJobs
Microsoft.Data.Tools.UnitTest
So please make sure that there is no red error lines on your code editor.
Then, close VS, delete .vs hidden folder under the solution folder, bin and obj folder.
After that, restart VS and your project, then, run update-package -reinstall command under Tools-->Nuget Package Manager-->Package Manager Console
Update 1
I think you have created a MSTest test project(net core) project rather than a unit test project(net framework) project.
In my side, I used unit test project(net framework), and all work well.
Actually, some of your nuget packages are for net framework. That is what the warning NU1701 did. But you can ignore that warning since it is normal that you have install the package into net core rather than net framework. And the package can be used under net core. And it is just a little warning rather than a error.
Due to that, update-package -reinstall cannot work for new-sdk project(your situaiton) and only for packages.config with net framework.
Maybe try these:
1)Besides, I think Microsoft.Data.Tools.Components.dll is from C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\Extensions\Microsoft\SQLDB. You have used some dlls from there. And due to some situations, these do not work and you should readd them on your current environment.
And first, please delete any references from here:
Then, readd these dlls from C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\Extensions\Microsoft\SQLDB to check if your project still have these errors.
Or you could remove Microsoft.Data.Tools.Components.dll from there to fix it.
If these do not help, maybe try nuget packages like what I said above:
1) clean all nuget caches first or just delete all files under C:\Users\xxx\.nuget\packages
2) to remove that warning, please add this xml node under csproj file of the unit test project.
3) Or you could try to use unit test project(net framework) project and then add your code there and I ensure that could work.

What is wrong with this code. Why can't I use SqlConnection?

I am 100% newbie to SQl and wanted to make a ConsoleApp with the use of database. I read some about it and tried. When I needed to make SqlConnection, my VS 2019 Preview showed me this
Severity Code Description Project File Line Suppression State
Error
CS1069 The type name 'SqlConnection' could not be found in the namespace 'System.Data.SqlClient'.
This type has been forwarded to assembly 'System.Data.SqlClient, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly.
ConsoleApp1 C:\Users\User\Desktop\Bald Code\ConsoleApp1\ConsoleApp1\Program.cs 12
Active
i don't get why it doesn't work
Here's my code
using System;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string connectionString;
SqlConnection cnn;
}
}
}
If you just updated EntityFrameworkCore from version 2.x to 3.x and you're running into this, change your using statement to Microsoft.Data.SqlClient instead of System.Data.SqlClient.
If you're using EntityFrameworkCore.SqlServer it already has that as a dependency, so you shouldn't need to install it explicitly.
This Microsoft blog explains the change.
Assuming that you're using .NET Core - just add NuGet package: System.Data.SqlClient
Your .csproj might look similar to:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.5.1" />
</ItemGroup>
</Project>
Most likely the System.Data.SqlClient.DLL in not in the Bin folder, and you have not set reference to the DLL in the project. You have missed the database choice when You install visual studio. And now You just manually add references named "System.Data.SqlClient".
1.right click you project name and choose nuget package options.
2.search "System.Data.SqlClient" and install it.
Hope this is fix Your Error
The specific way to fix this from within VS Code is to
Open a terminal by going to Terminal -> New Terminal
Run dotnet add package System.Data.SqlClient
Run dotnet restore
That last command might not be necessary, but doing this made it work for me. It seems that console app templates don't come prepared for a reference to SqlClient.
Update
I think moving forward projects should use Microsoft.Data.SqlClient and not System.Data.SqlClient: https://devblogs.microsoft.com/dotnet/introducing-the-new-microsoftdatasqlclient/.
As #Mmm mentioned in the comments if you are using .NET Core and have already installed the System.Data.SqlClient package, closing and reopening the project resolved the issue for me too.
I had that error today and what's happening its that VS CODE its no recognizing System.Data.SqlClient;, that's why you can't call the variable...
How did I fix it??
I installed:
SQL server compact toolbox
Nuget Package version updater
Close visual studio/Open it again and your program should be rocking and popping!!
[!["Manage Nuget Package for Solution"]
it fixed my issue.
[1]: https://i.stack.imgur.com/hpk2e.png
I just installed nuget package from the "Manage Nuget Package for Solution". Previously I had used through command but that did not work so I used the UI to install this package.
NuGet Package
Install-Package System.Data.SqlClient
solve my problem.
For me System.Data.SqlClient was already installed. What i had to do was, I went to the Nuget package manager and downgrade the version to 4.8.2 from 4.8.3 and it worked.
It's pretty simple
Go to solution explorer > right click & click manage NuGet packages > and install System.data.SqlClient 4.5.1. or later.
Click here to screenshot

Ninject.Web.Mvc.FluentValidation and FluentValidation.MVC4

I have tried setting up Ninject.Web.Mvc.FluentValidation in my ASP.NET MVC4 project
as shown below:
var ninjectValidatorFactory = new NinjectValidatorFactory(kernel);
//ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new NinjectValidatorFactory(kernel)));
FluentValidationModelValidatorProvider.Configure(x => x.ValidatorFactory = ninjectValidatorFactory);
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
but I am getting the below errors:
Error 1 The type 'FluentValidation.ValidatorFactoryBase' is defined in an assembly that is not referenced. You must add a reference to assembly 'FluentValidation, Version=2.0.0.0, ...
Error 2 Cannot implicitly convert type 'Ninject.Web.Mvc.FluentValidation.NinjectValidatorFactory' to 'FluentValidation.IValidatorFactory'. An explicit conversion exists (are you missing a cast?)
I can see the NuGet Package for Ninject.Web.Mvc.FluentValidation is version 3.0.0.0
and the error is telling me it is looking for version 2 of FluentValidation for its dependency.
If I install the NuGet package just for Ninject.Web.Mvc.FluentValidation then it installs the FluentValidation v2 dependency rather than version 3.4.6
but I can also see here: https://github.com/ninject/ninject.web.mvc.fluentvalidation/commit/82096d0afd15c41d01c09fd47f4247682261768e
a note about the project being updated for version 3.4.6
I am currently using NuGet to add the FluentValidation.MVC4 package to my project and then adding Ninject.Web.Mvc.FluentValidation after it (which finds it's dependency already exists and doesn't add it).
I can see that the NuGet package was last published on the 1st April 2013 but when I look at the .nuspec file in my project it shows
<dependency id="FluentValidation" version="3.2.0.0" />
Is there anything I have done wrong with this setup? or a way I can get this working through the package manager console to update the dependency version?
UPDATE: The reason for this is because Ninject.Web.Mvc.FluentValidation is using the signed version of FluentValidation which is strongly named.
David Ebbo has a good post about strong names and binding redirects here
http://blog.davidebbo.com/2011/01/nuget-versioning-part-3-unification-via.html
I still did not bother doing this to fix my problem, the NuGet package only has 1 class, the NinjectValidatorFactory, so it's worth just implementing your own.
To fix my problem for now I have put the NinjectValidatorFactoryclass into my own project. This looks to me like the current NuGet package needs to be updated further before it uses FluentValidation 3.4.6.
enter these codes in Package Manager Console :
Install-Package FluentValidation-Signed
Install-Package FluentValidation.MVC3-Signed -IgnoreDependencies
Install-Package Ninject.Web.Mvc.FluentValidation -IgnoreDependencies
-IgnoreDependencies is important .

EF 6 System.Data.Objects.ObjectContext Error

I recently upgraded from Entities Framework 5 to Entities Framework 6 Alpha 2 and I am getting the following error:
Method not found: 'System.Data.Objects.ObjectContext
System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext()'.
This is getting hit when I call
if (Membership.ValidateUser(model.UserName, model.Password)) {}
This used to work fine before not sure why it's springing this error. Any suggestions?
EF 6 does not have System.Data.Objects.ObjectContext. EF 6 has moved some types, including ObjectContext, from System.Data.Entity.dll into EntityFramework.dll, and changed their namespaces. The fact that you get this error suggests you haven't attempted to recompile your application, you've simply replaced EntityFramework.dll and hoped for the best. That won't work. You need to update your code to work with EF 6: you need to remove your references to System.Data.Entity.dll, and update your code to refer to the new types.
It just might be possible for the reference to the IObjectContextAdapter.ObjectContext property to be in some library you're using, but most likely it'll be in your own code. The error message (in the part you didn't include in your question) should tell you where it is coming from.
For me updating these below worked:
using System.Data.Objects; --> using System.Data.Entity.Core.Objects;
using System.Data.Objects.DataClasses; --> using System.Data.Entity.Core.Objects.DataClasses;
I'm also using EF 6.
I managed to solve the problem uninstalling the package Microsoft.AspNet.Providers.Core v. 1.2. I'm using version 1.1 instead. If you're like me and is using LocaDb, you'll have to uninstall the LocaDb package because it depends on that package. Of course you'll have to reinstall LocaDb again...
You can grab v. 1.1 using the NuGet Package Manager Console inside Visual Studio:
Install-Package Microsoft.AspNet.Providers.Core -Version 1.1
There's a Microsoft Connect bug filled regarding this issue:
Microsoft.AspNet.Providers.Core incompatible with EF6
The new 2.0 version of the providers (http://www.nuget.org/packages/Microsoft.AspNet.Providers.Core/) are EF6 compatible (they'll actually only work with EF6).
I managed to resolve this by removing the AspNet Providers I had installed through Nuget, which was marked as deprecated. Doing this also uninstalled Entity Framework.
I then installed the new AspNet Universal Providers, followed by Entity Framework 6, and all my problems were fixed.
Check This Link
http://visualstudiomagazine.com/articles/2014/03/01/whats-new-in-entity-framework-6.aspx
I Updated the EF 6.2 and get same error and find the solution as fallows
Change the namespace System.Data.Entity to System.Data.Entity.Core, including any references to System.Data.* namespaces (for example, System.Data.Objects becomes System.Data.Entity.Core.Objects).
That happens when entity framework is unable to find the method in the dotnet framework library installed in the machine. So install dotnet framework 4.5.2 or higher. It will fix the issue.
What worked for me was the following:
Install the dll 'Microsoft.AspNet.DataSource' with:
PM> Install-Package Microsoft.AspNet.EntityDataSource
Reference 'Microsoft.AspNet.DataSource.dll' in your project.
Added the following using declarations:
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Core.Objects;
using Microsoft.AspNet.EntityDataSource;
Removed the following using declarations:
using System.Data.Entity;
using System.Data.Metadata.Edm;
using System.Data.Objects;
using System.Web.UI.WebControls;
Voila, code is compiling and working.
It has an old version associated with edmx file for this:
Reinstall EF with Nuget
Delete the .edmx file and recreate it with tables
A quick and simple fix for me was to remove the offending assemblies (deprecated) and added a reference to the new library. The code was modified within the Context.tt and looks like this:
if (container.FunctionImports.Any())
{
#>
using System.Data.Entity.Core.Objects; // The assembly you need
using System.Linq;
<#
}
Before any modifications it had appeared as such:
if (container.FunctionImports.Any())
{
#>
using System.Data.Objects; // Error on compile
using System.Data.Objects.DataClasses; // Error on compile
using System.Linq;
<#

Categories

Resources