当我使用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; }
}
我希望有人能告诉我我能做些什么来解决这个问题?我会非常感谢<3