“The data reader has more than one field” error in Entity Framework

前端 未结 4 1707
我寻月下人不归
我寻月下人不归 2020-12-10 00:42

I\'m executing this simple query with Entity Framework

db.Database.SqlQuery(\"SELECT * FROM hospital\");

But I got this error

4条回答
  •  隐瞒了意图╮
    2020-12-10 01:30

    You might also get this error if you are trying to execute an INSERT, UPATE or DELETE command

    Instead of using SqlQuery use ExecuteSqlCommand

    using (var ctx = new SchoolDBEntities())
    {
        int noOfRowUpdated = ctx.Database.ExecuteSqlCommand("Update student 
                set studentname ='changed student by command' where studentid=1");
    
        int noOfRowInserted = ctx.Database.ExecuteSqlCommand("insert into student(studentname) 
                values('New Student')");
    
        int noOfRowDeleted = ctx.Database.ExecuteSqlCommand("delete from student 
                where studentid=1");
    }
    

    For more details visit - http://www.entityframeworktutorial.net/EntityFramework4.3/raw-sql-query-in-entity-framework.aspx

提交回复
热议问题