Engineering/Coding Standards/C#/C# Entity Framework/

C# Entity Framework Standards · CS-09

Eager-load and return an IEnumerable rather than IQueryable when querying a data set · CS-09.1 · SHOULD

Using IQueryable can lead to unnecessary database hits (and hard-to-diagnose performance issues) as it’s unclear when the query has actually been executed.

Preferably querying methods should expose results as an ICollection or IList as required.

Roslyn Analyzer Rule AV1250


Call AsNoTracking() when loading entities that will not be updated in the lifetime of the current DbContext instance · CS-09.2 · MUST

Entity tracking adds a performance overhead which is unnecessary with disconnected entities that are just being queried, not updated.

Specify a maximum length for strings when configuring an entity · CS-09.3 · MUST

EF will default to nvarchar(max) if a length is not specified. This can cause SQL server to calculate a much higher memory grant than needed to execute stored procedures, which will have an impact on performance.

NOTE This configuration can be applied by overriding the ConfigureConventions method in the class derived from DbContext. Learn more about pre-convention configuration in EF Core here.