How can I configure Entity Framework to automatically trim values retrieved for specific columns mapped to char(N) fields?

Rowan Miller (program manager for Entity Framework at Microsoft) recently posted a good solution to this which uses Interceptors. Admittedly this is only valid in EF 6.1+. His post is about trailing strings in joins, but basically, the solution as applied neatly removes trailing strings from all of the string properties in your models, automatically, … Read more

How can I catch UniqueKey Violation exceptions with EF6 and SQL Server?

With EF6 and the DbContext API (for SQL Server), I’m currently using this piece of code: try { // Some DB access } catch (Exception ex) { HandleException(ex); } public virtual void HandleException(Exception exception) { if (exception is DbUpdateConcurrencyException concurrencyEx) { // A custom exception of yours for concurrency issues throw new ConcurrencyException(); } else … Read more

Async PartialView causes “HttpServerUtility.Execute blocked…” exception

Child actions must be invoked synchronously. Issue 601 I am also not aware of any recent updates to the current MVC libraries allowing this functionality. A comment on Issue 601, hints at this functionality being added in MVC vNext, aka. MVC6. Child actions look to be replaced with ViewComponent which can be invoked asynchronously from … Read more

How to create index in Entity Framework 6.2 with fluent configuration

Well 26.10.2017 Entity Framework 6.2 was officially released. It includes a possibility to define indexes with ease via Fluent API. Ho it is to use was already announced in the beta of 6.2. Now you can use the HasIndex() method, followed by IsUnique() if it should be an unique index. Just a small comparison (before/after) … Read more

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

This is a topic I’m very interested in. There are many purists who say that you shouldn’t test technologies such as EF and NHibernate. They are right, they’re already very stringently tested and as a previous answer stated it’s often pointless to spend vast amounts of time testing what you don’t own. However, you do … Read more

There is already an object named in the database

it seems there is a problem in migration process, run add-migration command in “Package Manager Console”: Add-Migration Initial -IgnoreChanges do some changes, and then update database from “Initial” file: Update-Database -verbose Edit: -IgnoreChanges is in EF6 but not in EF Core, here’s a workaround: https://stackoverflow.com/a/43687656/495455

Entity Framework 6 transaction rollback

You don’t need to call Rollback manually because you are using the using statement. DbContextTransaction.Dispose method will be called in the end of the using block. And it will automatically rollback the transaction if the transaction is not successfully committed (not called or encountered exceptions). Following is the source code of SqlInternalTransaction.Dispose method (DbContextTransaction.Dispose will … Read more