Entity Framework Core 2.0 中使用LIKE 操作符
Entity Framework Core 2.0 中使用LIKE 操作符
不定时更新翻译系列,此系列更新毫无时间规律,文笔菜翻译菜求各位看官老爷们轻喷,如觉得我翻译有问题请挪步原博客地址
本博文翻译自:
http://gunnarpeipman.com/2017/08/ef-core-like-operator/
Entity Framework Core 2.0 早前发布,它带来了一些新的、很酷的特性。其中一个是SQL like操作符,我们现在可以直接在代码中使用它。下面是一个简单的web应用程序,它演示了使用新的like操作符。
我们先在SQL Server 数据库中建一个简单的歌曲表。

下面是表和数据库上下文的模型。
public class Song
{
public int Id { get; set; }
public string Artist { get; set; }
public string Title { get; set; }
public string Location { get; set; }
}
public class PlaylistContext : DbContext
{
public PlaylistContext(DbContextOptions<PlaylistContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Song>()
.HasKey(e => e.Id);
base.OnModelCreating(modelBuilder);
}
public virtual DbSet<Song> Songs { get; set; }
}
我跳过配置数据库连接和在启动类中需要做的所有事情,因为网上有很多关于这些的例子。
让我们从主控制器的歌曲表中查询一些歌曲,让我们把结果写出来。看一下索引方法中的linq 查询这是使用新的like操作符的地方。
public class HomeController : Controller
{
private readonly PlaylistContext _context;
public HomeController(PlaylistContext context)
{
_context = context;
}
public IActionResult Index()
{
var query = from s in _context.Songs
where EF.Functions.Like(s.Title, "%angel%")
select s;
return View(query);
}
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
索引视图还需要进行一些更改,以打印所找到的歌曲列表。
@model IEnumerable<Song>
@{
ViewData["Title"] = "Angel songs";
}
<div class="row">
<div class="col-md-4">
<h2>Songs</h2>
<table class="table table-bordered">
<thead>
<tr>
<th>Artist</th>
<th>Title</th>
</tr>
</thead>
<tbody>
@foreach (var song in Model)
{
<tr>
<td>@song.Artist</td>
<td>@song.Title</td>
</tr>
}
</tbody>
</table>
</div>
</div>
现在让我们运行程序看看它返回什么。

下面是由Entity Framework Core 2.0生成的sql查询,如果您需要看到EF生成的SQL,请参考这篇博客Entity Framework Core 2.0 Trace Strings.
SELECT [s].[Id], [s].[Artist], [s].[Location], [s].[Title]
FROM [Songs] AS [s]
WHERE [s].[Title] LIKE N'%angel%'
这是它。like操作符好像十分有魅力,生成的SQL看起来也不错。
总结
在Entity Framework Core 2.0中使用like操作符很容易。有特殊的功能类EF.Functions。它为我们提供了同类型的运算符,但没有更多的功能。我不知道接下来会发生什么,但似乎这个类将会在以后扩展到其他的方法,这些方法可以帮助我们保持对LINQ级别的查询,避免编写直接的SQL。
欢迎转载,转载请注明翻译原文出处(本文章),原文出处(原博客地址),然后谢谢观看
如果觉得我的翻译对您有帮助,请点击推荐支持:)
Entity Framework Core 2.0 中使用LIKE 操作符的更多相关文章
- Entity Framework Core 2.0 全局查询过滤器
不定时更新翻译系列,此系列更新毫无时间规律,文笔菜翻译菜求各位看官老爷们轻喷,如觉得我翻译有问题请挪步原博客地址 本博文翻译自: http://gunnarpeipman.com/2017/08/ef ...
- .Net Core 2.0生态(4):Entity Framework Core 2.0 特性介绍和使用指南
前言 这是.Net Core 2.0生态生态介绍的最后一篇,EF一直是我喜欢的一个ORM框架,随着版本升级EF也发展到EF6.x,Entity Framework Core是一个支持跨平台的全新版本, ...
- 【EF】Entity Framework Core 2.0 特性介绍和使用指南
阅读目录 前言 获取和使用 新特性 项目升级和核心API变化 下一步计划 遗憾的地方 回到目录 前言 这是.Net Core 2.0生态生态介绍的最后一篇,EF一直是我喜欢的一个ORM框架,随着版本升 ...
- Entity Framework Core 2.0 使用代码进行自动迁移
一.前言 我们在使用EF进行开发的时候,肯定会遇到将迁移更新到生产数据库这个问题,前面写了一篇文章介绍了Entity Framework Core 2.0的入门使用,这里面介绍了使用命令生成迁移所需的 ...
- ASP.Net Core项目在Mac上使用Entity Framework Core 2.0进行迁移可能会遇到的一个问题.
在ASP.Net Core 2.0的项目里, 我使用Entity Framework Core 2.0 作为ORM. 有人习惯把数据库的连接字符串写在appSettings.json里面, 有的习惯写 ...
- ASP.NET Core 1.0、ASP.NET MVC Core 1.0和Entity Framework Core 1.0
ASP.NET 5.0 将改名为 ASP.NET Core 1.0 ASP.NET MVC 6 将改名为 ASP.NET MVC Core 1.0 Entity Framework 7.0 将 ...
- [转帖]2016年时的新闻:ASP.NET Core 1.0、ASP.NET MVC Core 1.0和Entity Framework Core 1.0
ASP.NET Core 1.0.ASP.NET MVC Core 1.0和Entity Framework Core 1.0 http://www.cnblogs.com/webapi/p/5673 ...
- Entity Framework Core 2.0 使用入门
一.前言 Entity Framework(后面简称EF)作为微软家的ORM,自然而然从.NET Framework延续到了.NET Core.以前我也嫌弃EF太重而不去使用它,但是EF Core(E ...
- Entity Framework Core 2.0 入门简介
不多说废话了, 直接切入正题. EF Core支持情况 EF Core的数据库Providers: 此外还即将支持CosmosDB和 Oracle. EFCore 2.0新的东西: 查询: EF.Fu ...
随机推荐
- 【JAVASCRIPT】React学习-组件生命周期
摘要 整理组件加载过程,详细见官方文档:https://facebook.github.io/react/docs/react-component.html mount 过程 1)constructo ...
- 【TOP】top命令的load average的意义
一. 怎么查看机器负载 uptime 命令: $uptime 14:32:32 up 108 days, 23:04, 17 users, load average: 0.06, 0.08, 0.0 ...
- AD7928
实验室板子soc-de1自带的7928芯片,下面记录一下它的参数 吞吐速率 : 1MSPS 吞吐速率 : 是指ADC器件单位时间内能处理的任务数或输出结果的数量.单位:SPS(Samples per ...
- UE4 C++BeginPlay And BlueprintBeginPlay
今天遇到了一个诡异的问题,经过几个小时的煎熬终于找到了原因.mmmp 如果有一个类AActorChild,这个AActorChild继承自AActor,再有一个蓝图类BPAActorChild. 蓝图 ...
- C++ STL 双端队列deque详解
一.解释 Deque(双端队列)是一种具有队列和栈的性质的数据结构.双端队列的元素可以从两端弹出,其限定插入和删除操作在表的两端进行. 二.常用操作: 1.头文件 #include <deque ...
- HDU5742 It's All In The Mind(思维题,水题)
Problem Description Professor Zhang has a number sequence a1,a2,...,an. However, the sequence is not ...
- PocScan的搭建与使用
安装Docker, 然后下载镜像 $ sudo curl -sSL https://get.daocloud.io/docker | sh $ sudo systemctl start docker ...
- JAVA,JSP,Servlet获取当前工程路径-绝对路径
在jsp和class文件中调用的相对路径不同. 在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getPro ...
- Mongodb启动&关闭
mac 下mongo的启动和关闭以及启动问题解决 mongo的安装在这:http://www.cnblogs.com/leinov/p/6855784.html Mac os mongodb数据安装路 ...
- HDU字符串基础题(1020,1039,1062,1088,1161,1200,2017)
并不是很精简,随便改改A过了就没有再简化了. 1020. Problem Description Given a string containing only 'A' - 'Z', we could ...