Questions tagged [entity-framework]

Entity Framework is a LINQ-based object-database mapper for .NET. It supports change tracking, updates, and schema migrations for many databases. Please add a version-specific tag, when applicable.

Entity Framework is .NET's Object-Relational Mapping (ORM) tool that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write. Either natively, or through third-party libraries, it supports most major RDBM products including SQL Server, MySQL, Oracle, PostgreSQL and SQLite. It also supports Microsoft's "LINQ" syntax and lambda-expressions.

Entity Framework was first developed for .Net framework (production-ready versions 4.x - 6.x). In 2016, Entity Framework for .Net core (EF-core) was introduced, having a completely new codebase which shares many concepts with the classic framework but differs considerably in mapping syntax, query translation and specific features. After version 1 and 2, EF-core 3 came with many breaking changes, marking the beginning of a more stable evolution path. EF core 5 (versions keep track with .Net core versions) has far less breaking changes.

As of version 6.3.0, EF 6 is cross-platform. It targets .Net standard 2.1. NuGet packages are available for .Net core 3 and .NET Framework 4.x.

Because of all these different versions it's very important to use the right tags when asking questions.

Entity Framework is well-documented.

86622 questions
843
votes
17 answers

Entity Framework vs LINQ to SQL

Now that .NET v3.5 SP1 has been released (along with VS2008 SP1), we now have access to the .NET entity framework. My question is this. When trying to decide between using the Entity Framework and LINQ to SQL as an ORM, what's the difference? The…
Chris Roberts
  • 17,954
  • 11
  • 55
  • 66
837
votes
29 answers

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details

I am having this error when seeding my database with code first approach. Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. To be honest I don't know how to check the content of the validation…
Luis Valencia
  • 27,032
  • 76
  • 237
  • 423
734
votes
32 answers

Fastest Way of Inserting in Entity Framework

I'm looking for the fastest way of inserting into Entity Framework. I'm asking this because of the scenario where you have an active TransactionScope and the insertion is huge (4000+). It can potentially last more than 10 minutes (default timeout of…
Bongo Sharp
  • 8,720
  • 8
  • 22
  • 35
705
votes
43 answers

MetadataException: Unable to load the specified metadata resource

All of a sudden I keep getting a MetadataException on instantiating my generated ObjectContext class. The connection string in App.Config looks correct - hasn't changed since last it worked - and I've tried regenerating a new model (edmx-file) from…
J. Steen
  • 14,900
  • 15
  • 57
  • 62
701
votes
18 answers

There is already an open DataReader associated with this Command which must be closed first

I have this query and I get the error in this function: var accounts = from account in context.Accounts from guranteer in account.Gurantors select new AccountsReport { CreditRegistryId…
DotnetSparrow
  • 25,248
  • 60
  • 171
  • 307
688
votes
22 answers

How do I view the SQL generated by the Entity Framework?

How do I view the SQL generated by entity framework ? (In my particular case I'm using the mysql provider - if it matters)
nos
  • 207,058
  • 53
  • 381
  • 474
683
votes
11 answers

How can I retrieve Id of inserted entity using Entity framework?

I have a problem with Entity Framework in ASP.NET. I want to get the Id value whenever I add an object to database. How can I do this? According to Entity Framework the solution is: using (var context = new EntityContext()) { var customer = new…
ahmet
  • 6,863
  • 3
  • 13
  • 3
638
votes
20 answers

SqlException from Entity Framework - New transaction is not allowed because there are other threads running in the session

I am currently getting this error: System.Data.SqlClient.SqlException: New transaction is not allowed because there are other threads running in the session. while running this code: public class ProductManager : IProductManager { #region…
Keith Barrows
  • 23,420
  • 26
  • 78
  • 127
631
votes
10 answers

Code-first vs Model/Database-first

What are the pros & cons of using Entity Framework 4.1 Code-first over Model/Database-first with EDMX diagram? I'm trying to fully understand all the approaches to building data access layer using EF 4.1. I'm using Repository pattern and IoC. I know…
571
votes
35 answers

No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'

After downloading the EF6 by nuget and try to run my project, it returns the following error: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the…
Fernando Vellozo
  • 5,895
  • 2
  • 11
  • 12
563
votes
22 answers

Unable to update the EntitySet - because it has a DefiningQuery and no element exist

I am using Entity Framework 1 with .net 3.5. I am doing something simple like this: var roomDetails = context.Rooms.ToList(); foreach (var room in roomDetails) { room.LastUpdated = DateTime.Now; } I am getting this error when I try to…
iKode
  • 8,501
  • 13
  • 49
  • 76
479
votes
14 answers

EF Migrations: Rollback last applied migration?

This looks like a really common task, but I can't find an easy way to do it. I want to undo the last applied migration. I would have expected a simple command, like PM> Update-Database -TargetMigration:"-1" Instead, all I can come up with is: PM>…
438
votes
34 answers

Entity Framework Provider type could not be loaded?

I am trying to run my tests on TeamCity which is currently installed on my machine. System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer,…
ashutosh raina
  • 8,700
  • 10
  • 40
  • 80
434
votes
5 answers

Entity Framework VS LINQ to SQL VS ADO.NET with stored procedures?

How would you rate each of them in terms of: Performance Speed of development Neat, intuitive, maintainable code Flexibility Overall I like my SQL and so have always been a die-hard fan of ADO.NET and stored procedures but I recently had a play…
BritishDeveloper
  • 12,481
  • 7
  • 49
  • 59
421
votes
9 answers

Entity Framework - Include Multiple Levels of Properties

The Include() method works quite well for Lists on objects. But what if I need to go two levels deep? For example, the method below will return ApplicationServers with the included properties shown here. However, ApplicationsWithOverrideGroup is…
Bob Horn
  • 30,755
  • 28
  • 102
  • 197
1
2 3
99 100