Questions tagged [entity-framework-6]

Do NOT use this tag for Entity Framework Core 6 questions. This tag is for questions about Entity Framework 6, an ORM first released in 2013 and most recently updated to EF6.4 in 2019. It is not the same product as EF Core 6, first released in 2021

Entity Framework Version 6 (EF6) includes the following new features and changes.

  • Async Query and Save - EF6 now supports the task-based asynchronous patterns that were introduced in .NET 4.5. We've put together a walkthrough that demonstrates this new feature. You can also view the feature specification on our CodePlex site for more detailed information.
  • Custom Code First Conventions - You can now write custom conventions for Code First to help avoid repetitive configuration. We provide a simple API for lightweight conventions as well as some more complex building blocks to allow you to author more complicated conventions. There is a walkthough that covers both of these options and a feature specification on our CodePlex site.
  • Multi-Tenant Migrations - In previous versions of EF you were limited to one Code First model per database when using Migrations, this limitation is now removed. If you want to know more about how we enabled this, check out the feature specification on CodePlex.
  • Configurable Migrations History Table - Some database providers require the appropriate data types etc. to be specified for the Migrations History table to work correctly. The feature specification provides details about how to do this in EF6.
  • Code-Based Configuration - Configuration has traditionally been specified in a config file, EF6 also gives you the option of performing configuration in code. We've put together an overview with some examples and there is a feature specification with more details.
  • Dependency Resolution - EF now supports the Service Locator pattern and we've factored out some pieces of functionality that can be replaced with custom implementations. The feature specification provides details about this pattern, and we've put together a list of services that can be injected.
  • Updated Provider Model - In previous versions of EF some of the core components were a part of the .NET Framework. In EF6 we've moved all these components into our NuGet package, allowing us to develop and deliver more features in a shorter time frame. This move required some changes to our provider model. We've created a document that details the changes required by providers to support EF6, and provided a list of providers that we are aware of with EF6 support.
  • Enums, Spatial and Better Performance on .NET 4.0 - By moving the core components that used to be in the .NET Framework into the EF NuGet package we are now able to offer enum support, spatial data types and the performance improvements from EF5 on .NET 4.0.
10953 questions
452
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
  • 9,228
  • 12
  • 44
  • 80
320
votes
20 answers

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, BookTitle = _book.BookTitle, }; using (var…
user1327064
  • 4,187
  • 5
  • 20
  • 30
267
votes
14 answers

How to connect to LocalDB in Visual Studio Server Explorer?

I can't believe I couldn't find a working solution to this after an hour of searching. I'm following this article on Entity Framework 6.0 which gives a simple walk-through on Code First. I created the project and installed the latest EF Nuget…
194
votes
6 answers

Setting unique Constraint with fluent API?

I'm trying to build an EF Entity with Code First, and an EntityTypeConfiguration using fluent API. creating primary keys is easy but not so with a Unique Constraint. I was seeing old posts that suggested executing native SQL commands for this, but…
kob490
  • 3,177
  • 4
  • 25
  • 40
185
votes
12 answers

How are people unit testing with Entity Framework 6, should you bother?

I am just starting out with Unit testings and TDD in general. I have dabbled before but now I am determined to add it to my workflow and write better software. I asked a question yesterday that kind of included this, but it seems to be a question on…
Modika
  • 6,192
  • 8
  • 36
  • 44
157
votes
5 answers

Mapping composite keys using EF code first

Sql server table: SomeId PK varchar(50) not null OtherId PK int not null How should I map this in EF 6 code first? public class MyTable { [Key] public string SomeId { get; set; } [Key] public int OtherId { get; set; } } I've seen…
loyalflow
  • 14,275
  • 27
  • 107
  • 168
151
votes
4 answers

DbSet.Attach(entity) vs DbContext.Entry(entity).State = EntityState.Modified

When I am in a detached scenario and get a dto from the client which I map into an entity to save it I do this: context.Entry(entity).State = EntityState.Modified; context.SaveChanges(); What is the DbSet.Attach(entity) used for then? or why should…
Elisabeth
  • 20,496
  • 52
  • 200
  • 321
143
votes
31 answers

There is already an object named in the database

Update-Database failed from Package Manager Console. I've used Entity Framework 6.x and code-first approach. Error is "There is already an object named 'AboutUs' in the database." How can I solve this problem? internal sealed class Configuration …
130
votes
11 answers

How to create index in Entity Framework 6.2 with fluent configuration

Is there a way to create an index on a property/column using fluent configuration, instead of using the new IndexAttribute ?
Valo
  • 1,872
  • 2
  • 15
  • 23
117
votes
9 answers

Lazy Loading vs Eager Loading

Under what situation could eager loading be more beneficial than lazy loading? Lazy loading in Entity Framework is the default phenomenon that happens for loading and accessing the related entities. However, eager loading is referred to the practice…
Arnold Zahrneinder
  • 4,788
  • 10
  • 40
  • 76
107
votes
2 answers

Database.BeginTransaction vs Transactions.TransactionScope

What is the difference between System.Transactions.TransactionScope and EF6's Database.BeginTransaction? Could someone give a small example or just explain which one to use when with a clear difference? P.S: In my project, I'm using EF6. I've…
91
votes
3 answers

Async PartialView causes "HttpServerUtility.Execute blocked..." exception

I have a partial view that tries to retrieve a IEnumerable from the database using async... Method public static class PostService { public static int PostsPerPage = 50; public static async Task> GetRecentAsync(int…
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
90
votes
3 answers

Entity Framework 6 transaction rollback

With EF6 you have a new transaction which can be used like: using (var context = new PostEntityContainer()) { using (var dbcxtransaction = context.Database.BeginTransaction()) { try { …
89
votes
3 answers

Multi-async in Entity Framework 6?

This is my code: var banner = context.Banners.ToListAsync() var newsGroup = context.NewsGroups.ToListAsync() await Task.WhenAll(banner, newsGroup); But when i called the function from controller. It showed error A second operation started on this…
An Hv
  • 897
  • 1
  • 6
  • 7
83
votes
11 answers

Entity Framework 6 GUID as primary key: Cannot insert the value NULL into column 'Id', table 'FileStore'; column does not allow nulls

I have an entity with primary key "Id" which is Guid: public class FileStore { public Guid Id { get; set; } public string Name { get; set; } public string Path { get; set; } } And some configuration: protected override void…
Algirdas
  • 1,065
  • 1
  • 9
  • 21
1
2 3
99 100