.NET Tools How-To's

Visualize Entity Framework Relationships and Additional Query Analysis in ReSharper 2023.3

A lot of teams are using Entity Framework or EF Core to work with their database. As an Object-Relational Mapper (ORM), it bridges objects in code to a relational database model, so that as a developer you don’t have to worry too much about the actual database.

We all know: that’s not entirely true. Even with Entity Framework and EF Core, you still have to add indexes so data can be retrieved quickly, be wary about the N+1 problem, and many other potential pitfalls.

The latest ReSharper 2023.3 comes with a number of tools and inspections to help you catch potential issues with Entity Framework and EF Core, including a way to generate an Entity Relationship Diagram (ERD) for your database context. Let’s have a look.

Entity Relationship Diagram

The most visual update in ReSharper 2023.3 is the new Entity Relationship Diagram (ERD) that you can use to inspect how entities are related to each other. When you open any entity that is part of an Entity Framework or EF Core database context in the editor, you will see that ReSharper identifies it as such:

When you press Alt+Enter, you can then use the Show Entity Relationship Diagram action to generate an ERD for the current database context. Clicking the database icon also works, and there’s the ReSharper | Architecture | Show Entity Relationship Diagram menu as well.

ReSharper generates a graphic representation of the database model and the relationships between entities, including one-to-one, one-to-many and many-to-many relationships. You can pan and zoom, and look at the various entities in your database context.

The diagram also highlights string properties with unlimited length. A perfect segue to the next section!

Unlimited string length inspection

One of the new inspections introduced in ReSharper 2023.3 analyzes your database context and its entities for properties that have unlimited string length, and highlights them:

To fix this inspection, you can add a [StringLength] or [MaxLength] attribute to specify the length of the string in the database model. Alternatively, you can use the HasMaxLength() builder method in your database context model creation logic:

public class Database : DbContext
    // ...

    protected override void OnModelCreating(ModelBuilder builder)
        // Country
        builder.Entity<Country>().Property(ci => ci.Name)

In SQL Server and Azure SQL Database, an unbounded string with unlimited length will be stored in a nvarchar(max) column. When you know the data will never exceed a certain length, limiting the string length in your EF model will translate into the data being stored in an nvarchar(N) column.

Now, why does this matter? By default, SQL Server stores data in pages on disk where each row and its data can be 8060 bytes in size. Large object data types (LOB) such as xml, varbinary(max), and varchar(max), are stored in a separate location in SQL Server. When a row is too large (or has many LOBs), SQL Server can not read data in one go and has to look in several places to filter and return the result set for your query, resulting in additional I/O overhead.

In MySQL and MariaDB, row size is constrained to 65535 bytes, so limiting how long a row can get is crucial to keeping your database and its performance happy!

The new unlimited string length inspection helps you discover properties that have the potential to impact database performance.

DbFunction inspections

Another new inspection helps detect usages of methods in Entity Framework-related code that could potentially produce runtime exceptions. Whenever you use a function that is not convertible to SQL, ReSharper will let you know about this.

Additionally, when using methods that are only to be used in a database context, such as functions in the EF.Functions property, ReSharper informs you that the database function must not be called in non-database context.

These 2 new inspections will make sure that potential exceptions in executing (non-)database queries are discovered even before running your application.

In conclusion

Being able to inspect the relationships between entities in an Entity Relationship Diagram (ERD) is a great opportunity for finding missing relationships or dependency loops in a graphical way. 

Combined with the ability to find properties that may negatively impact query performance, and to find usages of functions that do not belong in an Entity Framework query, this ReSharper release will help you build more solid applications using Entity Framework and EF Core. 

And of course, don’t forget the Entity Framework and EF Core N+1 inspections introduced in the previous ReSharper and JetBrains Rider releases!

What do you think of the new ERD and inspections? Let us know in the comments!

image description

Discover more