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

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>

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.

Prevent inheritance of incompatible configuration section in child application

Project A
uses entity framework 4.4.0.0 and has below entry in web.config
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
Project B
uses entity framework 6.0.0.0 and has below entry in web.config
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
Project B is hosted under Project A in IIS (Project B was virtual directory, later converted to web application)
If I access project B, it shows message in browser that there is duplicate entry of entity framework.
I commented entry in project B
<!--<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />-->
This makes Project B run, but as soon it access data from database, an exception is thrown.
An error occurred creating the configuration section handler for entityFramework: Could not load file or assembly 'EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies.
I cant edit Project A EF or Project B EF, as I don't have access
Any suggestions on how to deal with this?
You can do that by disallowing inheritance of the section. To do so, you can add this atribute to what shouldn't be inherited in the parent web.config: inheritInChildApplications="false"
As explained here not all elements suport it, so perhaps you have to do it in a parent, and redefined all the other things that should be inherited: How to stop inheritance of <configSections>in Web.Config This is a good solution for it: Avoid web.config inheritance in child web application using inheritInChildApplications
You can also see the 7th tip of this asp.net log: 10 Things ASP.NET Developers Should Know About Web.config Inheritance and Overrides

Umbraco gets error on dev server

I just published my local version to a dev server. My local is working perfectly, but the dev has an error:
Could not load file or assembly 'System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Does anybody know how can I fix it?
I started to use VS 2015, because of nuget version. Now I got some different error in the output box:
No way to resolve conflict between "System.Net.Http.Formatting, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" and "System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35". Choosing "System.Net.Http.Formatting, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" arbitrarily.
1> Consider app.config remapping of assembly "System.Net.Http.Formatting, Culture=neutral, PublicKeyToken=31bf3856ad364e35" from Version "5.0.0.0" [] to Version "5.2.3.0" [F:\Projects\frs\frs\bin\System.Net.Http.Formatting.dll] to solve conflict and get rid of warning.
1> Consider app.config remapping of assembly "System.Web.WebPages, Culture=neutral, PublicKeyToken=31bf3856ad364e35" from Version "1.0.0.0" [] to Version "2.0.0.0" [F:\Projects\frs\frs\bin\System.Web.WebPages.dll] to solve conflict and get rid of warning.
1> Consider app.config remapping of assembly "System.Web.Mvc, Culture=neutral, PublicKeyToken=31bf3856ad364e35" from Version "3.0.0.0" [] to Version "4.0.0.0" [F:\Projects\frs\frs\bin\System.Web.Mvc.dll] to solve conflict and get rid of warning.
Maybe this will help
https://our.umbraco.org/forum/getting-started/installing-umbraco/53820-Could-not-load-file-or-assembly-SystemNetHttp-after-upgrade-to-v-621
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
to this:
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
I had the same problem with Umbraco. I have installed Umbraco v7.4.3 and I tried the Ucalendar Package in it.
You have to remove the Package and restore the app_data and config folders.

entity framework code first migrations from seperate windows class library

i have a solution that contains an MVC project and windows class library project that uses entity framework and is a data access layer. I tried to enable migrations with the following package manager console line
Enable-Migrations -ProjectName PortlandRoad.DAL -ContextTypeName PortlandRoadDBContext -Force
I try to update the database using the following line
Update-Database -Verbose -Force
if i do this with the mvc project as the startup project it works, but uses the connection string in the mvc project web.config. If i do this with the dAL project as the startup project i get the following error :
A file activation error occurred. The physical file name '\PortlandRoadDB.mdf' may be incorrect. Diagnose and correct additional errors, and retry the operation.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
the app.config file for my DAL project is as follows :
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!--<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />-->
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=PortlandRoadDB;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\PortlandRoadDB.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
can anybody tell me how to correct this so that the migrations work specifically for the dal project and are not dependent on the MCV project being the startup project
thanks
I've the same issue in a WPF project.
I think that you have two options.
Option 1
At startup in you windows project try to specify the correct path to your database using
AppDomain.CurrentDomain.SetData("DataDirectory","your\physical\path");
With this, you will have the same error but your migrations can still being applied to your principal database.
option 2
Find a way to replace dynamically this |DataDirectory| in your app.config file before starting your application. (.bat , or .ps or ...).
another option: The bad one.
Before running your migration change the path to you DataDirectory to a physical temporary one in your app.config (this file will help you keep up to date migrations history)
before launching your application replace the temporary file path by |DataDirectory| and change this parameter in your startup with the App.config
With this work around, if your are planning to deploy your application, you will keep track of all migrations in order to anticipate futures database modifications.
VoilĂ .
Could it be that you're using "|DataDirectory|" in the connection string in the DAL project? Since "|DataDirectory|" is an Asp.Net folder and the DAL project is not an Asp project it probably doesn't recognize that as a directory and it probably doesn't exist thing the DAL project.

PowerTools Beta 3 fails

I was trying to generate "views" to enhance the performance of my application. The application is Silverlight 4 linked with RIA services to the Server. The server application started with EF 4.1 + RIA Services.
I installed "EF Power Tools Beta 3 v0.7.0.0" and could generate my views with no problem. But I soon realized that EF 4.1. was not able to benefit from the precompiled views. EF >= 4.2 was required.
Through NuGet I saw an update for RIA Services (RIAServices.EntityFramework.4.2.0) which used EF 5. I installed and got everything working but when I try to generate views I get an exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
I've tried reinstalling PowerTools. No luck
I've downloaded source code for PowerTools and debugged the Exception. Apparently PowerTools was trying and could not find the old EF version. The LoaderExceptions property read:
{System.IO.FileNotFoundException: Could not load file or assembly 'EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies.
I've investigated the PublicKeyToken and confirmed that it is the same as the EF 5 (EF 4.4 target=.NET 4) dll found in my "bin" output map. But PowerTools is looking for EF 4.1.. why?
My web.config file has this:
<sectionGroup name="system.serviceModel">
<section name="domainServices" type="System.ServiceModel.DomainServices.Hosting.DomainServicesSection, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" allowDefinition="MachineToApplication" requirePermission="false" />
</sectionGroup>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
and:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.4.0.0" newVersion="4.4.0.0" />
</dependentAssembly>
</assemblyBinding>
I've also tested changing the PublicKeyToken in Web.config just to confirm that I get another error from PowerTools thus PowerTools seems to parse my Web.config.
Update:
I've created a new Web project and added all Model code files as links. Same connectionstrings. Now i can build views.. strange.. same references.
When I add the views to my original project the RIA services won't start. Removed views and they started again.