I have a problem when I am using SqlQuery
method to try to get some data from database, everything is perfect if I get to all column like this:
data.Items.SqlQuery("SELECT * FROM dbo.Items WHERE iID = '1' ORDER BY iTitle")
But I just want to get maybe one or two columns, but that does not work and I get an error:
data.Items.SqlQuery("SELECT iTitle FROM dbo.Items WHERE iID = '1' ORDER BY iTitle")
Error:
Additional information: The data reader is incompatible with the specified '....Models.Item'. A member of the type, 'iID', does not have a corresponding column in the data reader with the same name.
Below is some code of model and 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; }
}
I hope someone can tell me what I can do to fix this problem? I'd be extremely grateful for that <3
if your are using entity framework with code first approach you can use below code implementation to get data
then you can use
list for whatever you want. hope this will help you!