Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法先在SqlQuery-实体框架代码中选择特定列_C#_Entity Framework 6_Ef Code First_Rawsql - Fatal编程技术网

C# 无法先在SqlQuery-实体框架代码中选择特定列

C# 无法先在SqlQuery-实体框架代码中选择特定列,c#,entity-framework-6,ef-code-first,rawsql,C#,Entity Framework 6,Ef Code First,Rawsql,当我使用SqlQuery方法试图从数据库中获取一些数据时,我遇到了一个问题,如果我像这样访问所有列,一切都很完美: data.Items.SqlQuery("SELECT * FROM dbo.Items WHERE iID = '1' ORDER BY iTitle") 但我只想得到一到两列,但这不起作用,我得到一个错误: data.Items.SqlQuery("SELECT iTitle FROM dbo.Items WHERE iID = '1' ORDER BY iTitle")

当我使用
SqlQuery
方法试图从数据库中获取一些数据时,我遇到了一个问题,如果我像这样访问所有列,一切都很完美:

data.Items.SqlQuery("SELECT * FROM dbo.Items WHERE iID = '1' ORDER BY iTitle")
但我只想得到一到两列,但这不起作用,我得到一个错误:

data.Items.SqlQuery("SELECT iTitle FROM dbo.Items WHERE iID = '1' ORDER BY iTitle")
错误:

其他信息:数据读取器与指定的“……Models.Item”不兼容。“iID”类型的成员在数据读取器中没有同名的对应列

下面是模型和dbcontext的一些代码

[Table("Items")]
public class Item
{
    [Key]
    [Column("iID")]
    public int iID { get; set; }

    [Column(TypeName = "varchar")]
    [StringLength(50)]
    public int iTitle { get; set; }
}

public class ItemDBContext : DbContext
{
    public ItemDBContext() : base("name=connectionstring") { }
    public DbSet<Item> Items { get; set; }
}
[表格(“项目”)]
公共类项目
{
[关键]
[列(“iID”)]
公共整数iID{get;set;}
[列(TypeName=“varchar”)]
[长度(50)]
公共整数{get;set;}
}
公共类ItemDBContext:DbContext
{
public ItemDBContext():base(“name=connectionstring”){}
公共数据库集项{get;set;}
}

我希望有人能告诉我我能做些什么来解决这个问题?我非常感谢如果您使用实体框架和代码优先的方法,您可以使用下面的代码实现来获取数据

var itemdata = data.Items.Where(x=> x.iID.Equals(1)).OrderBy(x=> x.iTitle).toList();
然后你可以用

项目数据


列出你想要的任何东西。希望这对你有帮助

var items=data.items.Where(item=>item.iID==1)。选择(item=>item.iTitle)您正在使用实体框架-使用其查询功能并停止使用原始SQL:。。。。