Get entity navigation properties after insert

If I understand you correctly, you’re trying to eagerly load a complex property after establishing a relationship via a foreign key property. SaveChanges() does not do anything in the way of loading complex properties. At most, it is going to set your primary key property if you’re adding new objects. Your line reward = context.Set<Reward>().SingleOrDefault(a … Read more

Log Queries executed by Entity Framework DbContext

Logging and Intercepting Database Operations article at MSDN is what your are looking for. The DbContext.Database.Log property can be set to a delegate for any method that takes a string. Most commonly it is used with any TextWriter by setting it to the “Write” method of that TextWriter. All SQL generated by the current context … Read more

How to sync model after using Code First from Database using Entity Framework 6.1 and MVC 5?

The Entity Data Model Wizard’s Code First from Database does an excellent job creating your entity classes, as if they were created in the Code First style. What you are asking is if there is any way to keep these classes up-to-date as your database changes, similar to the EDMX style “Update Model From Database”. … Read more

Does Entity Framework support parallel async queries? [duplicate]

This is not supported as per the specifications of version 6. This should throw a DbConcurrencyException exception saying A second operation started on this context before a previous asynchronous operation completed. Use ‘await’ to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to … Read more

How entity framework works for large number of records? [closed]

I faced a similar situation where we had a large database with many tables 7- 10 million records each. we used Entity framework to display the data. To get nice performance here’s what I learned; My 10 Golden rules for Entity Framework: Understand that call to database made only when the actual records are required. … Read more

Code-first migration: How to set default value for new property?

If you see the generated migration code you will see AddColumn AddColumn(“dbo.report”, “newProperty”, c => c.String(nullable: false)); You can add defaultValue AddColumn(“dbo.report”, “newProperty”, c => c.String(nullable: false, defaultValue: “old”)); Or add defaultValueSql AddColumn(“dbo.report”, “newProperty”, c => c.String(nullable: false, defaultValueSql: “GETDATE()”));

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

FindAsync and Include LINQ statements

The simplest is to use FirstOrDefaultAsync or SingleOrDefaultAsync instead: model.Item = await db.Items.Include(i => i.ItemVerifications) .FirstOrDefaultAsync(i => i.Id == id.Value); The reason you are getting the error is because Find / FindAsync methods are defined for DbSet<T>, but the result of Include is IQueryable<T>. Another way is to combine FindAsync with explicit loading: model.Item = … Read more

How to create a table corresponding to enum in EF6 Code First?

Since EF doesn’t handle it automatically, yes, this is the recommend way. I suggest some modifications in article that you provided. Rename your enum public enum FacultyEnum { Eng, Math, Eco } Create a class that represent the table public class Faculty { private Faculty(FacultyEnum @enum) { Id = (int)@enum; Name = @enum.ToString(); Description = … Read more