What’s the difference(s) between .ToList(), .AsEnumerable(), AsQueryable()?

I know some differences of LINQ to Entities and LINQ to Objects which the first implements IQueryable and the second implements IEnumerable and my question scope is within EF 5. My question is what’s the technical difference(s) of those 3 methods? I see that in many situations all of them work. I also see using … Read more

The relationship could not be changed because one or more of the foreign-key properties is non-nullable

I am getting this error when I GetById() on an entity and then set the collection of child entities to my new list which comes from the MVC view. The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a … Read more

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 my real example) doesn’t work. var firstName = “John”; var id = 12; var sql = @”Update [User] SET FirstName … Read more

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

Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago. Improve this question My impression to date has been that a DbContext is meant to represent your database, and … Read more

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; set;} public int SecondColumn { get; set;} } I want to make the combination between FirstColumn and SecondColumn as unique. Example: Id FirstColumn SecondColumn 1 1 1 = OK 2 2 … Read more

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 it doesn’t generate a full file (it’s empty – because I … Read more

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

Closed. This question needs debugging details. It is not currently accepting answers. Want to improve this question? Update the question so it’s on-topic for Stack Overflow. Closed 4 years ago. The community reviewed whether to reopen this question 6 months ago and left it closed: Original close reason(s) were not resolved Improve this question I … Read more

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> Get-Migrations Retrieving migrations that have been applied to the target … Read more

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 ‘entityFramework’ section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information. 35 s 35