entity framework查询生成的语句,为什么没有参数化?
[Table("Sys_User", Schema = "dbo")]
public class User
{
[Key]
public int ID { get; set; }
[Display(Name = "账号")]
[StringLength(20)]
[Required]
public string Account { get; set; }
[Display(Name = "姓名")]
[StringLength(20)]
[Required]
public string Name { get; set; }
[Display(Name = "所属部门")]
public int? DeptID { get; set; }
[Display(Name = "性别")]
[StringLength(10)]
[Required]
public string Sex { get; set; }
[Display(Name = "密码")]
[StringLength(50)]
[Required]
public string Password { get; set; }
[Display(Name = "邮箱")]
[StringLength(50)]
public string Email { get; set; }
[Display(Name = "是否启用")]
[Required]
public bool Enabled { get; set; }
[Display(Name = "工作电话")]
[StringLength(50)]
public string OfficePhone { get; set; }
[Display(Name = "手机号")]
[StringLength(50)]
public string CellPhone { get; set; }
[Display(Name = "备注")]
[StringLength(100)]
public string Remark { get; set; }
[Display(Name = "上次登录时间")]
public DateTime? LastLoginTime { get; set; }
[Display(Name = "创建时间")]
public DateTime? CreateTime { get; set; }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace WebApplication1.Models
{
public class EFContext : DbContext
{
public EFContext() : base("Default")
{
}
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// 禁用默认表名复数形式
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
}
}
使用查询:
EFContext db = new EFContext();
Models.User u = db.Users.Where(p => p.Account == "admin").FirstOrDefault();
通过监听生成的SQL语句:
SELECT TOP (1)
[Extent1].[ID] AS [ID],
[Extent1].[Account] AS [Account],
[Extent1].[Name] AS [Name],
[Extent1].[DeptID] AS [DeptID],
[Extent1].[Sex] AS [Sex],
[Extent1].[Password] AS [Password],
[Extent1].[Email] AS [Email],
[Extent1].[Enabled] AS [Enabled],
[Extent1].[OfficePhone] AS [OfficePhone],
[Extent1].[CellPhone] AS [CellPhone],
[Extent1].[Remark] AS [Remark],
[Extent1].[LastLoginTime] AS [LastLoginTime],
[Extent1].[CreateTime] AS [CreateTime]
FROM [dbo].[Sys_User] AS [Extent1]
WHERE N'admin' = [Extent1].[Account]
为什么没有使用参数化查询条件呢,应该怎么配置,求教。
...全文
相关推荐
SqlServer:使用IN()子句C#进行参数化查询
使用参数化查询在SQL中为IN()运算符发送参数的实用工具类
怎样预防SQL注入?
好在要防止ASP.NET应用被SQL注入式攻击闯入并不是一件特别困难的事情,只要在利用表单输入的内容构造SQL命令之前,把所有输入内容过滤一番就可以了。过滤输入内容可以按多种方式进行。
entity framework 多表查询方式
文件全面的提供了多表查询的方式,相信会帮倒大家的。
防SQL注入 生成参数化的通用分页查询语句
前些时间看了玉开兄的“如此高效通用的分页存储过程是带有sql注入漏洞的”这篇文章,才突然想起某个项目也是使用了累似的通用分页存储过程。
Entity Framework官方中文教程
Entity Framework微软官方教程,800多页,含EF Core,EF 6等


