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
415
votes
9 answers

One DbContext per web request... why?

I have been reading a lot of articles explaining how to set up Entity Framework's DbContext so that only one is created and used per HTTP web request using various DI frameworks. Why is this a good idea in the first place? What advantages do you…
Andrew
  • 10,088
  • 14
  • 48
  • 62
404
votes
14 answers

The entity cannot be constructed in a LINQ to Entities query

There is an entity type called Product that is generated by entity framework. I have written this query public IQueryable GetProducts(int categoryID) { return from p in db.Products where p.CategoryID== categoryID …
Ghooti Farangi
  • 19,228
  • 14
  • 42
  • 61
400
votes
26 answers

Conversion of a datetime2 data type to a datetime data type results out-of-range value

I've got a datatable with 5 columns, where a row is being filled with data then saved to the database via a transaction. While saving, an error is returned: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range…
Gerbrand
  • 4,724
  • 4
  • 27
  • 34
392
votes
9 answers

Why use ICollection and not IEnumerable or List on many-many/one-many relationships?

I see this a lot in tutorials, with navigation properties as ICollection. Is this a mandatory requirement for Entity Framework? Can I use IEnumerable? What's the main purpose of using ICollection instead of IEnumerable or even List?
Jan Carlo Viray
  • 11,088
  • 11
  • 40
  • 59
367
votes
9 answers

What are the best practices for using a GUID as a primary key, specifically regarding performance?

I have an application that uses GUID as the Primary Key in almost all tables and I have read that there are issues about performance when using GUID as Primary Key. Honestly, I haven't seen any problem, but I'm about to start a new application and I…
VAAA
  • 12,647
  • 20
  • 110
  • 213
367
votes
35 answers

Error message 'Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.'

I have developed an application using Entity Framework, SQL Server 2000, Visual Studio 2008 and Enterprise Library. It works absolutely fine locally, but when I deploy the project to our test environment, I am getting the following error: Unable to…
The Light
  • 24,407
  • 61
  • 164
  • 254
351
votes
3 answers

Using Transactions or SaveChanges(false) and AcceptAllChanges()?

I have been investigating transactions and it appears that they take care of themselves in EF as long as I pass false to SaveChanges() and then call AcceptAllChanges() if there are no errors: SaveChanges(false); // ... AcceptAllChanges(); What if…
mark smith
  • 19,527
  • 44
  • 131
  • 185
351
votes
13 answers

How to compare only Date without Time in DateTime types in Linq to SQL with Entity Framework?

Is there a way to compare two DateTime variables in Linq2Sql but to disregard the Time part. The app stores items in the DB and adds a published date. I want to keep the exact time but still be able to pull by the date itself. I want to compare…
Sruly
  • 9,480
  • 6
  • 30
  • 37
351
votes
10 answers

Entity Framework Timeouts

I am getting timeouts using the Entity Framework (EF) when using a function import that takes over 30 seconds to complete. I tried the following and have not been able to resolve this issue: I added Default Command Timeout=300000 to the connection…
Halcyon
  • 13,619
  • 17
  • 64
  • 91
341
votes
45 answers

Entity Framework: "Store update, insert, or delete statement affected an unexpected number of rows (0)."

I am using Entity Framework to populate a grid control. Sometimes when I make updates I get the following error: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since…
strongopinions
  • 3,787
  • 3
  • 23
  • 28
340
votes
17 answers

Validation failed for one or more entities while saving changes to SQL Server Database using Entity Framework

I want to save my Edit to Database and I am using Entity FrameWork Code-First in ASP.NET MVC 3 / C# but I am getting errors. In my Event class, I have DateTime and TimeSpan datatypes but in my database, I've got Date and time respectively. Could…
user522767
  • 3,943
  • 6
  • 19
  • 16
339
votes
24 answers

How do I delete multiple rows in Entity Framework (without foreach)

I'm deleting several items from a table using Entity Framework. There isn't a foreign key / parent object so I can't handle this with OnDeleteCascade. Right now I'm doing this: var widgets = context.Widgets .Where(w => w.WidgetId ==…
Jon Galloway
  • 50,160
  • 24
  • 120
  • 192
335
votes
17 answers

Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why?

I've been wrestling with this for a while and can't quite figure out what's happening. I have a Card entity which contains Sides (usually 2) - and both Cards and Sides have a Stage. I'm using EF Codefirst migrations and the migrations are failing…
SB2055
  • 10,654
  • 29
  • 87
  • 185
325
votes
2 answers

Ignoring a class property in Entity Framework 4.1 Code First

My understanding is that the [NotMapped] attribute is not available until EF 5 which is currently in CTP so we cannot use it in production. How can I mark properties in EF 4.1 to be ignored? UPDATE: I noticed something else strange. I got the…
Raheel Khan
  • 13,199
  • 12
  • 71
  • 154
324
votes
14 answers

Reset Entity-Framework Migrations

I've mucked up my migrations, I used IgnoreChanges on the initial migration, but now I want to delete all my migrations and start with an initial migration with all of the logic. When I delete the migrations in the folder and try and Add-Migration…
Todd
  • 14,946
  • 6
  • 42
  • 56