首页 > 其他分享 >upgrading-from-ef-core-6-to-7

upgrading-from-ef-core-6-to-7

时间:2023-08-22 11:46:25浏览次数:65  
标签:core EF Core Blogs ef upgrading context new method


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

March 7, 2023/0 Comments/in General development/by ajtowf

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:
    1 2 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:

    1 2 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:
    1 context.Blogs.Update(blog);

    EF Core 7:

    1 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:
    1 options.UseInternalServiceProvider(serviceProvider);

    EF Core 7:

    1 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:
    1 context.Database.ExecuteSqlInterpolated($"UPDATE dbo.Blogs SET Rating = {newRating} WHERE Url = {url}");

    EF Core 7:

    1 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:
    1 var sql = context.Blogs.Where(b => b.Url.StartsWith("https://")).ToSql();

    EF Core 7:

    1 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:
    1 var blogs = context.Blogs.AsNoTracking().ToList();

    EF Core 7:

    1 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:
    1 var context = new MyDbContext();

    EF Core 7:

    1 2 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:
    1 "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:

    1 "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.

Tags: .NETC#EF Core 6EF Core 7Entity Framework CoreORMhttps://www.towfeek.se/2023/03/07/breaking-changes-when-upgrading-from-ef-core-6-to-7-what-you-need-to-know/

标签:core,EF,Core,Blogs,ef,upgrading,context,new,method
From: https://www.cnblogs.com/Leo_wl/p/17648133.html

相关文章

  • Future和CompletableFuture区别
    Future:获取异步返回的结果需要使用轮询的方式,消耗cupExecutorServiceexecutorService=Executors.newFixedThreadPool(10);Future<String>future=executorService.submit(()->{try{Thread.sleep(2000);}......
  • Educational Codeforces Round 153 (Rated for Div. 2)
    Preface最近CF状态烂得一批,已经连续两场被D题腐乳了,再这样下去就真成抱队友大腿的混子了但没想到因为D题比赛时贪心过的人太多了,后面一波叉掉了比赛时过的\(\frac{1}{3}\)的人导致竟然还能上分我是没想到的没抓住暑假大好的上分机会,等开学后再想冲分就难咯A.NotaSubstring......
  • EFCore多数据库合并查询分页
    EFCore多数据库合并查询分页参照:二个表的数据如何做分页?_两个表排序分页_深圳市热心市民市民的博客-CSDN博客基本情况介绍:由于系统迭代,部分收藏表在老系统的数据库,部分在新api接口的数据库,现在有一个需求是在个人中心展示用户收藏的数据,按照收藏时间倒序排列,因为在APP端实际上......
  • 8.makefile-gdb-文件IO
    8.makefile-gdb-文件IO学习目标:熟练使用规则编写简单的makefile文件熟练使用makefile中的变量熟练使用makefile中的函数熟练掌握gdb相关调试命令的使用了解概念:pcb和文件描述符,虚拟地址空间熟练掌握Linux系统IO函数的使用1.makefilemakefile文件中定义了一系列的规则......
  • EF的三种加载方式
    一、三种加载方式   延迟加载【LazyLoading】、饥饿加载【EagerLoading】、显式加载【ExplicitLoading】二、区别     2.1、延迟加载:        说明:和淘宝商品列表一样,下拉刷新,按需加载。        特点:关闭延迟加载后,查询主表......
  • Codeforces Round 893 (Div. 2) A-C题解
    CF893(Div.2)A.Buttons签到题。两人会优先选择c中的按钮来,避免自己的按钮消耗同时减少对方可选择的按钮。所以c%2==1等价于a的按钮数+1,c%2==0时相当于c按钮不存在,比较ab按钮的数量来得出答案即可。#include<iostream>usingnamespacestd;typedeflonglong......
  • 在Vue3后台管理系统中使用watch和watcheffect
    ​ 1、watch在Vue3中的组合式API中,watch的作用和Vue2中的watch作用是一样的,他们都是用来监听响应式状态发生变化的,当响应式状态发生变化时,都会触发一个回调函数。constmessage=ref("test");watch(message,(newValue,oldValue)=>{console.log("新值:",ne......
  • Vue3 toRef响应式失效问题排查
    父组件conststuRecordInfo=ref<any>({stuNum:11111})//接口请求返回stuRecordInfo.value.stuNum=22222<StuRecord:info="stuRecordInfo":pictureInfo="pictureInfo"/>子组件constinfo=toRef(props,'info')consts......
  • EFZ暑训2023 挂分记录
    \(Day1\)\(20230731\):挂\(120pts\),\(120\to0\)(原因:Dev-C++保存炸了)\(Day2\)\(20230801\):挂\(60pts\),\(250\to190\)(原因:不能先pop再push)\(Day3\)\(20230802\):挂\(-20pts\),\(100\to120\)(原因:以为T180pts,实际100pts)\(Day4\)\(20230803\):挂......
  • vue中export default data()外面的数据如何通过$t获取动态值
    如果将数据放到外面的`const`中,而不是在Vue组件的`data`选项中,那么无法直接通过`$t`方法来获取翻译文件的值。因为`$t`方法是Vue-i18n插件提供的实例方法,需要在Vue组件中使用。如果你想在外部使用`$t`方法来获取翻译文件的值,可以将Vue-i18n的实例导出,并在外部文件中引入该实例来......