Ef Core 6.0 Add InitialCreate fails - entity-framework

I am using Ef Core 6.0, and when I try to run the first migration using
"dotnet ef migrations add InitialCreate" I get the following error.
Build started...
Build succeeded.
System.IO.FileLoadException: Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type, ObjectHandleOnStack keepAlive)
at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)
at Microsoft.EntityFrameworkCore.Tools.ReflectionOperationExecutor..ctor(String assembly, String startupAssembly, String projectDir, String dataDirectory, String rootNamespace, String language, Boolean nullable, String[] remainingArguments)
at Microsoft.EntityFrameworkCore.Tools.Commands.ProjectCommandBase.CreateExecutor(String[] remainingArguments)
at Microsoft.EntityFrameworkCore.Tools.Commands.MigrationsAddCommand.Execute(String[] args)
at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args)
at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)

Not sure if you still have this issue but in case you do. Try adding reference Microsoft.EntityFrameworkCore.Design to your project where db context is defined and that should resolve it.
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0"/>

Related

Postsharp compile error Ambiguous match System.Runtime.InteropServices

I'm trying to add PostSharp Logging to a .NET Framework app. I'm getting this error. How do I resolve it? Is there a way to tell PostSharp which type it should map to?
Unhandled exception (postsharp-x86-srv.exe 6.10.8 release | .NET Framework 4.8 (X86)): PostSharp.Sdk.CodeModel.BindingException:
Ambiguous match: need to map 'System.Runtime.InteropServices.ImportedFromTypeLibAttribute, System.Runtime.InteropServices, Culture=neutral, Context=Runtime' and have several candidates:
'System.Runtime.InteropServices.ImportedFromTypeLibAttribute, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, Context=Reference'
'System.Runtime.InteropServices.ImportedFromTypeLibAttribute, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, Context=Reference.

Could not load assembly System.Runtime.CompilerServices.Unsafe

I am working on .NET Core app + NET Standard 2 library.
This library uses EntityFrameworkCore.SQLServer.
When I added yet EntityFrameworkCore.PostgreSQL then app during the start gives me an error:
... Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExce
ptionInterceptor.MoveNext(CancellationToken cancellationToken)
System.IO.FileLoadException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, C
ulture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The located assembly's manifest definition does not match the assembly
reference. (Exception from HRESULT: 0x80131040)
File name: 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at Npgsql.NpgsqlWriteBuffer.Write[T](T value)
I have reinstalled the EntityFrameworkCore.PostgreSQL - not fixed.
May be I can add this assembly to my library but where I can get it:
'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, C
ulture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
Thanx.

System.IO.FileNotFoundException: Could not load assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

Getting this Error In Xamarin Form Android Project I am refering Entity framework library int android project. Error as below
Severity Code Description Project File Line Suppression State Error
Exception while loading assemblies: System.IO.FileNotFoundException:
Could not load assembly 'EntityFramework, Version=6.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089'. Perhaps it doesn't
exist in the Mono for Android profile? File name:
'EntityFramework.dll' at
Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.Resolve(AssemblyNameReference
reference, ReaderParameters parameters) at
Xamarin.Android.Tasks.ResolveAssemblies.AddAssemblyReferences(DirectoryAssemblyResolver
resolver, ICollection1 assemblies, AssemblyDefinition assembly,
Boolean topLevel) at
Xamarin.Android.Tasks.ResolveAssemblies.AddAssemblyReferences(DirectoryAssemblyResolver
resolver, ICollection1 assemblies, AssemblyDefinition assembly,
Boolean topLevel) at
Xamarin.Android.Tasks.ResolveAssemblies.AddAssemblyReferences(DirectoryAssemblyResolver
resolver, ICollection`1 assemblies, AssemblyDefinition assembly,
Boolean topLevel) at
Xamarin.Android.Tasks.ResolveAssemblies.Execute(DirectoryAssemblyResolver
resolver) RadLoc.Android C:\Program Files (x86)\Microsoft Visual
Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets
1500
I am stuck here please help me out of this..
Just like #magicandre1981 said. Entity Framework doesn't fit in Xamarin.Android project. You need to use EntityFramework Core.
For a simple jump in and brief introduction, you can refer to Building Android Apps with Entity Framework.
For a windows application with this error:
If your copying the exe to a different folder from /bin/
You also need to include a copy of the application .config file along with the EntityFramework.xml and EntityFramework.SqlServer.xml files.

'System.ValueTuple, Version=0.0.0.0 required for Add-Migration on .NET 4.6.1 Class Library

I upgraded a .net standard class library from Entity Framework Core 1.1 to Entity Framework 2.0
I am trying to run Add-Migration on an Entity Framework Core Class Library that targets .net framework 4.6.1
Add-Migration MyMigration
But I then get the following error
System.IO.FileLoadException: Could not load file or assembly 'System.ValueTuple, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'System.ValueTuple, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.SharedTableConvention.Apply(InternalModelBuilder modelBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnModelBuilt(InternalModelBuilder modelBuilder)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory)
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass22_0.b__0(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure1 accessor)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func1 factory)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
I added the nuget package of System.ValueTuple to my csproj but I still get the error
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
Add AutoGenerateBindingRedirects and GenerateBindingRedirectsOutputType to your class library csproj
e.g.
<PropertyGroup>
<TargetFramework>net461</TargetFramework>
....
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
I had the same problem, and it got fixed after installing .NET 4.7.1 framework
Starting with the following lovely error because StructureMap failed to instantiate the dbContext for Entity Framework Core:
An error occurred when trying to create a controller of type '[NameOfController]'. Make sure that the controller has a parameterless public constructor.
I had to do some more work than the answers supplied here to get it to work in .NET Framework 4.6.2:
Install nugets (source: https://github.com/neuecc/MessagePack-CSharp/issues/46)
System.Threading.Tasks.Extensions (I used latest v4.5.2)
System.ValueTuple (I used old 4.3.1)
Add to .proj that contained repositories (source: https://stackoverflow.com/a/45978409/581414)
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
And to web.config (sources: various)
<configuration>
...
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
...
<dependentAssembly>
<assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" />
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
...
</configuration>

Using Asp.net Identity in with Entity Framework throws Could not load type .Schema.IndexAttribute' from assembly 'EntityFramework, Version=6.0

public EmployeeDTO AuthenticateEmployee(string username, string password)
{
try
{
var userLogin = UnitOfWork.UserLoginRepository.Get(x => x.UserName == username).FirstOrDefault();
if (userLogin == null)
return null;
else
{
var userStore = new UserStore<IdentityUser>();
var userManager = new UserManager<IdentityUser>(userStore);
// this throws an error.
var user = userManager.Find("username", "password");
}
}
}
Results in error:
Could not load type 'System.ComponentModel.DataAnnotations.Schema.IndexAttribute' from assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
I'm using EF 6.0, Microsoft.AspNet.Identity.EntityFramework Ver 2.0.
I couldn't perform any action based on EF using Identity is there anything I have to do, my EDMX is in another class library. I think its a DLL issue.
Please help me to use identity with EF.
I have gone through Msdn
IndexAttribute is one of the new things included in EF 6.1. So the problem is that you're referencing the wrong EF version (6.0). Please reference 6.1 in your project.
Look at EF Version History.
I also had this error but all the EntityFramework references in packages.config were pointing to the latest EF version (in my case 6.1.3). I unloaded the offending project where the error was occuring from and noticed the Hint paths for EF were old. After updating them, they worked fine.
Old References:
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.SqlServer.dll</HintPath>
<Private>True</Private>
</Reference>
Updated working references:
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll</HintPath>
<Private>True</Private>
</Reference>