Breaking Changes When Upgrading from EF Core 6 to 7: What You Need to Know

Entity Framework Core (EF Core) is a popular Object-Relational Mapping (ORM) framework used by .NET developers for database operations. With the release of EF Core 7, many developers are considering upgrading their projects to take advantage of the new features and improvements.

However, as with any major version upgrade, there are some breaking changes that developers need to be aware of. In this blog post, we’ll discuss some of the breaking changes when migrating from EF Core 6 to 7 and how to address them.

  1. Renaming of “FromSqlRaw” and “FromSqlInterpolated” methods
    In EF Core 7, the “FromSqlRaw” and “FromSqlInterpolated” methods have been renamed to “FromSql” and “FromSqlInterpolated”, respectively. This is a minor change, but if your project has a lot of code that uses these methods, you will need to update it to use the new method names.EF Core 6:

    var blogs = context.Blogs.FromSqlRaw("SELECT * FROM dbo.Blogs").ToList();
    var blogs = context.Blogs.FromSqlInterpolated($"SELECT * FROM dbo.Blogs WHERE Url = {url}").ToList();
    

    EF Core 7:

    var blogs = context.Blogs.FromSql("SELECT * FROM dbo.Blogs").ToList();
    var blogs = context.Blogs.FromSqlInterpolated($"SELECT * FROM dbo.Blogs WHERE Url = {url}").ToList();
    
  2. Changes to the “Update” method In EF Core 7, the “Update” method has been changed to use a “Set” method instead of “Update”. This means that if you have code that uses the “Update” method, you will need to update it to use the new “Set” method.EF Core 6:
    context.Blogs.Update(blog);
    

    EF Core 7:

    context.Blogs.Set<Blog>().Update(blog);
    
  3. Removal of the “UseInternalServiceProvider” method In EF Core 7, the “UseInternalServiceProvider” method has been removed. This method was used to configure the dependency injection container for EF Core, but it has been replaced with a new configuration method called “AddEntityFramework”.EF Core 6:
    options.UseInternalServiceProvider(serviceProvider);
    

    EF Core 7:

    options.AddEntityFramework().UseInternalServiceProvider(serviceProvider);
    
  4. Removal of the “ExecuteSqlInterpolated” method In EF Core 7, the “ExecuteSqlInterpolated” method has been removed. This method was used to execute SQL queries with interpolated string arguments. In EF Core 7, you can use the new “ExecuteSql” method with parameters to achieve the same functionality.EF Core 6:
    context.Database.ExecuteSqlInterpolated($"UPDATE dbo.Blogs SET Rating = {newRating} WHERE Url = {url}");
    

    EF Core 7:

    context.Database.ExecuteSqlRaw("UPDATE dbo.Blogs SET Rating = {0} WHERE Url = {1}", newRating, url);
    
  5. Changes to the “ToSql” method In EF Core 7, the “ToSql” method has been changed to “ToQueryString”. This method is used to generate SQL queries from LINQ expressions. If you have code that uses the “ToSql” method, you will need to update it to use the new “ToQueryString” method.EF Core 6:
    var sql = context.Blogs.Where(b => b.Url.StartsWith("https://")).ToSql();
    

    EF Core 7:

    var sql = context.Blogs.Where(b => b.Url.StartsWith("https://")).ToQueryString();
    
  6. Changes to the “AsNoTracking” method In EF Core 7, the “AsNoTracking” method has been changed to accept a “QueryTrackingBehavior” parameter. This parameter can be used to specify the tracking behavior for the query. If you have code that uses the “AsNoTracking” method, you will need to update it to include the new parameter.EF Core 6:
    var blogs = context.Blogs.AsNoTracking().ToList();
    

    EF Core 7:

    var blogs = context.Blogs.AsNoTracking(QueryTrackingBehavior.NoTracking).ToList();
    
  7. Changes to the “CreateDbContext” method In EF Core 7, the “CreateDbContext” method has been changed to accept a “DbContextOptions” parameter. This parameter can be used to configure the DbContext options. If you have code that uses the “CreateDbContext” method, you will need to update it to include the new parameter.EF Core 6:
    var context = new MyDbContext();
    

    EF Core 7:

    var options = new DbContextOptionsBuilder<MyDbContext>().UseSqlServer(connectionString).Options;
    var context = new MyDbContext(options);
    
  8. Default value of “Encrypt” attribute in SQL Server connection strings has been changed to “true” In EF Core 7, the default value for the “Encrypt” attribute in SQL Server connection strings has been changed to “true”. This is a high-impact breaking change as it may affect existing applications that rely on the previous default behavior. This means that if you are upgrading to EF Core 7 and your application relies on an unencrypted SQL Server connection, you will need to explicitly set the “Encrypt” attribute to “false” in your connection string.For example, you can modify the “Data Source” attribute in the connection string to include the “Encrypt=false” parameter, like this:
    "Data Source=myServer;Initial Catalog=myDatabase;Integrated Security=True;Encrypt=false;"
    

    Alternatively, you can set the “Encrypt” attribute explicitly to “true” if you wish to use an encrypted connection:

    "Data Source=myServer;Initial Catalog=myDatabase;Integrated Security=True;Encrypt=true;"
    

In summary, upgrading to EF Core 7 can bring many benefits to your project, but it’s important to be aware of the breaking changes that come with the upgrade. By understanding and addressing these changes, you can ensure a smooth migration and take full advantage of the new features and improvements in EF Core 7.

Angular Tip of the Day: Using Custom Pipes in Angular

Welcome to the Angular Tip of the Day! In this series, we’ll be sharing quick tips and tricks to help you get the most out of Angular and improve your development workflow.

Tip of the Day: Using Custom Pipes in Angular

One of the powerful features of Angular is the ability to create and use custom pipes. Pipes are a way to transform data in your templates, and can be used to filter, format, and manipulate data. In this post, we’ll explore how to create and use custom pipes in Angular.

Creating a Custom Pipe

To create a custom pipe in Angular, you need to define a class that implements the PipeTransform interface. This interface requires you to implement a transform method that takes an input value and returns a transformed value. Here’s an example:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'capitalize' })
export class CapitalizePipe implements PipeTransform {
  transform(value: string): string {
    if (!value) {
      return '';
    }

    return value.charAt(0).toUpperCase() + value.slice(1);
  }
}

This pipe takes a string input and capitalizes the first letter. It’s named ‘capitalize’ and can be used in templates by using the pipe symbol (|) and the pipe name:

{{ 'hello world' | capitalize }}

Using a Custom Pipe

To use a custom pipe in Angular, you need to add it to your module’s declarations array. Here’s an example:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent } from './app.component';
import { CapitalizePipe } from './capitalize.pipe';

@NgModule({
  declarations: [AppComponent, CapitalizePipe],
  imports: [BrowserModule],
  bootstrap: [AppComponent]
})
export class AppModule {}

Once the pipe is declared, you can use it in templates like this:

{{ 'hello world' | capitalize }}

That’s it! Custom pipes are a powerful tool in Angular that can help you transform data in your templates. By creating your own pipes, you can extend the built-in functionality of Angular and create reusable code that can be used throughout your application.

We hope you found this Angular Tip of the Day useful. Stay tuned for more tips and tricks to help you become an Angular master!