Questions tagged [entity-framework]

For questions about Entity Framework for .net framework, max. version 6.4, December 2019. Use [entity-framework-core] for current .net (core) versions. Then also please add a version-specific [ef-core-xx] tag.

Filter by
Sorted by
Tagged with
878 votes
29 answers
939k views

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

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 ...
Luis Valencia's user avatar
868 votes
17 answers
293k views

Entity Framework vs LINQ to SQL [closed]

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 ...
Chris Roberts's user avatar
821 votes
12 answers
674k views

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 ...
ahmet's user avatar
  • 8,243
810 votes
26 answers
542k views

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's user avatar
  • 227k
808 votes
32 answers
580k views

Fastest Way of Inserting in Entity Framework

I'm looking for the fastest way of inserting in Entity Framework. I'm asking this because of the scenario where you have an active TransactionScope and the insertion is huge (4000+). It can ...
Bongo Sharp's user avatar
  • 9,530
796 votes
21 answers
680k views

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 ...
DotnetSparrow's user avatar
731 votes
47 answers
554k views

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 ...
J. Steen's user avatar
  • 15.5k
687 votes
23 answers
282k views

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 ...
Keith Barrows's user avatar
656 votes
10 answers
355k views

Code-first vs Model/Database-first [closed]

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 ...
Jakub Konecki's user avatar
604 votes
22 answers
407k views

Unable to update the EntitySet - because it has a DefiningQuery and no <UpdateFunction> 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 = ...
Positonic's user avatar
  • 9,291
600 votes
35 answers
462k views

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'....
Fernando Vellozo's user avatar
567 votes
18 answers
542k views

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 -...
Cristian Diaconescu's user avatar
520 votes
10 answers
395k views

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 ...
Bob Horn's user avatar
  • 33.9k
461 votes
28 answers
498k views

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 ...
Gerbrand's user avatar
  • 5,354
454 votes
34 answers
314k views

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....
ashutosh raina's user avatar
449 votes
10 answers
233k views

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

I see this a lot in tutorials, with navigation properties as ICollection<T>. Is this a mandatory requirement for Entity Framework? Can I use IEnumerable? What's the main purpose of using ...
Jan Carlo Viray's user avatar
445 votes
5 answers
187k views

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

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 ...
BritishDeveloper's user avatar
444 votes
9 answers
119k views

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 ...
Andrew's user avatar
  • 11.2k
431 votes
9 answers
227k views

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

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 ...
VAAA's user avatar
  • 14.8k
424 votes
14 answers
296k views

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<Product> GetProducts(int categoryID) { return from p in db.Products ...
Ghooti Farangi's user avatar
424 votes
20 answers
387k views

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 ...
SB2055's user avatar
  • 12.6k
413 votes
38 answers
512k views

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 ...
The Light's user avatar
  • 26.7k
409 votes
12 answers
417k views

How to compare only Date without Time in DateTime types?

Is there a way to compare two DateTime variables 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 ...
Sruly's user avatar
  • 10.4k
396 votes
27 answers
431k views

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

I want to delete several items from a table using Entity Framework. There is no foreign key / parent object, so I can't handle this with OnDeleteCascade. Right now I'm doing this: var widgets = ...
Jon Galloway's user avatar
  • 52.7k
394 votes
10 answers
416k views

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 ...
Halcyon's user avatar
  • 14.8k
376 votes
18 answers
306k views

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 ...
Kind Contributor's user avatar
376 votes
2 answers
298k views

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: ...
Raheel Khan's user avatar
  • 14.4k
367 votes
4 answers
307k views

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: ...
mark smith's user avatar
  • 20.8k
366 votes
25 answers
388k views

Entity Framework. Delete all rows in table

How can I quickly remove all rows in the table using Entity Framework? I am currently using: var rows = from o in dataDb.Table select o; foreach (var row in rows) { dataDb.Table.Remove(...
Zhenia's user avatar
  • 4,049
357 votes
45 answers
411k views

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

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). ...
strongopinions's user avatar
344 votes
6 answers
295k views

What difference does .AsNoTracking() make?

I have a question regarding the .AsNoTracking() extension, as this is all quite new and quite confusing. I'm using a per-request context for a website. A lot of my entities don't change so don't ...
dotnetnoob's user avatar
  • 11.1k
339 votes
17 answers
325k views

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 ...
user522767's user avatar
  • 4,023
336 votes
17 answers
220k views

Entity Framework: There is already an open DataReader associated with this Command

I am using Entity Framework and occasionally i will get this error. EntityCommandExecutionException {"There is already an open DataReader associated with this Command which must be closed first."} ...
Sonic Soul's user avatar
  • 24.4k
329 votes
20 answers
899k views

How to update record using Entity Framework 6?

I am trying to update a record using EF6. First finding the record, if it exists, update. Here is my code: var book = new Model.Book { BookNumber = _book.BookNumber, BookName = _book.BookName,...
user1327064's user avatar
  • 4,277
329 votes
7 answers
154k views

Create code first, many to many, with additional fields in association table

I have this scenario: public class Member { public int MemberID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public virtual ICollection&...
hgdean's user avatar
  • 3,293
326 votes
12 answers
264k views

Unique Key constraints for multiple columns in Entity Framework

I'm using Entity Framework 5.0 Code First; public class Entity { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public string EntityId { get; set;} public int FirstColumn { get;...
Bassam Alugili's user avatar
323 votes
11 answers
311k views

Entity Framework Core add unique constraint code-first

I can't find way to add a unique constraint to my field with using attribute: public class User { [Required] public int Id { get; set; } [Required] // [Index("IX_FirstAndSecond", 2, ...
Vadim M's user avatar
  • 3,285
319 votes
7 answers
226k views

Entity Framework Code First - two Foreign Keys from same table

I've just started using EF code first, so I'm a total beginner in this topic. I wanted to create relations between Teams and Matches: 1 match = 2 teams (home, guest) and result. I thought it's ...
Jarek's user avatar
  • 5,945
307 votes
22 answers
593k views

How to call Stored Procedure in Entity Framework 6 (Code-First)?

I am very new to Entity Framework 6 and I want to implement stored procedures in my project. I have a stored procedure as follows: ALTER PROCEDURE [dbo].[insert_department] @Name [varchar](100) ...
Jaan's user avatar
  • 3,373
300 votes
9 answers
108k views

Should services always return DTOs, or can they also return domain models?

I'm (re)designing large-scale application, we use multi-layer architecture based on DDD. We have MVC with data layer (implementation of repositories), domain layer (definition of domain model and ...
Robert Goldwein's user avatar
296 votes
9 answers
332k views

Non-static method requires a target

I have a controller action that works fine on Firefox both locally and in production, and IE locally, but not IE in production. Here is my controller action: public ActionResult MNPurchase() { ...
user547794's user avatar
  • 14.4k
290 votes
19 answers
255k views

Decimal precision and scale in EF Code First

I'm experimenting with this code-first approach, but I'm find out now that a property of type System.Decimal gets mapped to a sql column of type decimal(18, 0). How do I set the precision of the ...
Dave Van den Eynde's user avatar
281 votes
11 answers
177k views

Entity Framework: One Database, Multiple DbContexts. Is this a bad idea? [closed]

My impression to date has been that a DbContext is meant to represent your database, and thus, if your application uses one database, you'd want only one DbContext. However, some colleagues want to ...
Josh Schultz's user avatar
  • 8,130
277 votes
3 answers
124k views

What does principal end of an association means in 1:1 relationship in Entity framework

public class Foo { public string FooId{get;set;} public Boo Boo{get;set;} } public class Boo { public string BooId{get;set;} public Foo Foo{get;set;} } I was trying to do this in ...
taher chhabrawala's user avatar
277 votes
4 answers
151k views

Entity Framework and Connection Pooling

I've recently started to use the Entity Framework 4.0 in my .NET 4.0 application and am curious about a few things relating to pooling. Connection pooling as I know is managed by the ADO.NET data ...
Noldorin's user avatar
  • 146k
271 votes
19 answers
365k views

Entity Framework 6 Code first Default value

is there "elegant" way to give specific property a default value ? Maybe by DataAnnotations, something like : [DefaultValue("true")] public bool Active { get; set; } Thank you.
marino-krk's user avatar
  • 2,721
269 votes
30 answers
283k views

No connection string named 'MyEntities' could be found in the application config file

I am using entity framework and ASP.NET MVC 4 to build an application My solution is split into two projects; A class library that includes my data model (.edmx) file and a few custom interfaces The ...
jjc99's user avatar
  • 3,559
268 votes
10 answers
147k views

Using MySQL with Entity Framework [closed]

Can't find anything relevant about Entity Framework/MySQL on Google so I'm hoping someone knows about it.
vintana's user avatar
  • 2,893
256 votes
12 answers
180k views

"A lambda expression with a statement body cannot be converted to an expression tree"

In using the EntityFramework, I get the error "A lambda expression with a statement body cannot be converted to an expression tree" when trying to compile the following code: Obj[] myArray = objects....
pistacchio's user avatar
252 votes
15 answers
230k views

How to pass parameters to the DbContext.Database.ExecuteSqlCommand method?

Let's just suppose I have a valid need for directly executing a sql command in Entity Framework. I am having trouble figuring out how to use parameters in my sql statement. The following example (not ...
jessegavin's user avatar
  • 74.9k

1
2 3 4 5
1839